Iterate over Characters of String in Java

In this post, we will discuss various methods to iterate over characters in a String in Java.


 

1. Naive

Naive solution would be to use a simple for loop to process each character of the String. This approach proves to be very effective for strings of smaller length.

Java

Download   Run Code

2. Using String.toCharArray()

We can also convert a String to char[] using String.toCharArray() function and then iterate over the character array using enhanced for loop (for-each loop) as shown below:

Java

Download   Run Code

3. Using Iterator

We can also use StringCharacterIterator class that implements bidirectional iteration for a String.

Java

Download   Run Code

4. Using StringTokenizer

Another solution would be to use StringTokenizer although its use is discouraged. The StringTokenizer class breaks a string into tokens. Its prototype is:

StringTokenizer(String str, String delim, boolean returnDelims)

An instance of StringTokenizer behaves in one of two ways, depending on whether it was created with the returnDelims flag having the value true or false:

  • If the flag is false, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters.
     
  • If the flag is true, delimiter characters are themselves considered to be tokens. A token is thus either one delimiter character, or a maximal sequence of consecutive characters that are not delimiters.

Java

Download   Run Code

5. Using String.Split() –

It is recommended to use the String.split() method over StringTokenizer which is a legacy class and still alive for compatibility reasons.

Java

Download   Run Code

6. Using Guava –

Guava’s Lists.charactersOf() returns a view of the specified string as an immutable list of characters. We can process the immutable list using for-each loop or an iterator. Please note that this method just returns a view, no actual copying happens here.

Java

Download   Run Code

7. Using String.chars() –

Java 8 provides a new method String.chars() which returns a IntStream (stream of ints) that represent an integer representation of characters in the String. This method does not return desired Stream<Character> (for performance reasons) but we can map IntStream to an object in such a way that it will automatically box into a Stream<Character>. There are various ways to that as shown below:

Java

Download   Run Code

8. Using Code Points –

We can also use Java 8 String.codePoints() instead of String.chars() that also returns an IntStream but having Unicode code points instead of char values.

Java

Download   Run Code

9. Using Reflection –

For very long strings, nothing beats Reflection in terms of Performance. We can inspect any string using Reflection and access the backing array of specified String. To find the name of the backing array, we can print all the Fields of String Class using below code and search one with type char[].

Java

Download   Run Code

 
Related Post: Iterate over a String backwards in Java

 
Thanks for reading.




Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 





Leave a Reply

Notify of
avatar
wpDiscuz