How can I iterate through a string in Java?
I'm trying to use a foreach style for loop
for (char x : examplestring) { //action } If you want to use enhanced loop, you can convert the string to charArray
for (char ch : exampleString.toCharArray()) { System.out.println(ch); } String to a temporary array that is returned by the function.length() and charAt()..lenght() and .charAt() for each char.char[] copy.Using Guava (r07) you can do this:
for(char c : Lists.charactersOf(someString)) { ... } This has the convenience of using foreach while not copying the string to a new array. Lists.charactersOf returns a view of the string as a List.
charAt test.Collection-based APIs than just for iteration like this.