+ 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

15th Jan 2018, 12:16 AM
A G
A G - avatar
3 Answers
+ 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.
15th Jan 2018, 1:05 AM
Deddy Tandean
+ 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
15th Jan 2018, 12:30 AM
Jacob Pembleton
Jacob Pembleton - avatar