StringTokenizer
? Convert the String
to a char[]
and iterate over that? Something else?
If you need to iterate through the code points of a String
(see this answer) a shorter / more readable way is to use the CharSequence#codePoints method added in Java 8:
for(int c : string.codePoints().toArray()){
...
}
or using the stream directly instead of a for loop:
string.codePoints().forEach(c -> ...);
There is also CharSequence#chars if you want a stream of the characters (although it is an IntStream
, since there is no CharStream
).