+ 2
Is there a way to read a string character by character?
I'm trying to write some code so that you can give it an equation and have it rearranged to solve for any variable. Right now my biggest hiccup is splitting one string into seperate characters. I'm still somewhat of a beginner in java so I have no idea how to do this
3 Réponses
+ 3
Use for loop to loop through each character and then use charAt() to print character by character.
For example:
String str = "Hello";
for (int i = 0; i < str.length(); i++){
System.out.println (str.charAt (i));
}
// Output:
H
e
l
l
o
If you want to print side by side, simply use:
System.out.print ()
Hope this helps.
+ 2
I don’t know if this works in Java (it probably will), but in C++ you can treat a string as an array of characters and access them like this:
string str = “hello”;
char ch = str[1];
// ch = e