What is the easiest/best/most correct way to iterate through the characters of a string in Java?
Some ways to iterate through the characters of a string in Java are:
- Using
StringTokenizer
? - Converting the
String
to achar[]
and iterating over that.
What is the easiest/best/most correct way to iterate?
Solution 1:
I use a for loop to iterate the string and use charAt()
to get each character to examine it. Since the String is implemented with an array, the charAt()
method is a constant time operation.
String s = "...stuff...";
for (int i = 0; i < s.length(); i++){
char c = s.charAt(i);
//Process char
}
That's what I would do. It seems the easiest to me.
As far as correctness goes, I don't believe that exists here. It is all based on your personal style.
Solution 2:
Two options
for(int i = 0, n = s.length() ; i < n ; i++) {
char c = s.charAt(i);
}
or
for(char c : s.toCharArray()) {
// process c
}
The first is probably faster, then 2nd is probably more readable.
Solution 3:
Note most of the other techniques described here break down if you're dealing with characters outside of the BMP (Unicode Basic Multilingual Plane), i.e. code points that are outside of the u0000-uFFFF range. This will only happen rarely, since the code points outside this are mostly assigned to dead languages. But there are some useful characters outside this, for example some code points used for mathematical notation, and some used to encode proper names in Chinese.
In that case your code will be:
String str = "....";
int offset = 0, strLen = str.length();
while (offset < strLen) {
int curChar = str.codePointAt(offset);
offset += Character.charCount(curChar);
// do something with curChar
}
The Character.charCount(int)
method requires Java 5+.
Source: http://mindprod.com/jgloss/codepoint.html