Iterate over characters of a String in Java

Google Translate Icon

This post will discuss various methods to iterate over characters in a string in Java.

1. Naive solution

A naive solution is 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.

Download  Run Code

2. Using String.toCharArray() method

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

Download  Run Code

3. Using Iterator

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

Download  Run Code

4. Using StringTokenizer

Another solution is 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 returnDelims is false, delimiter characters serve to separate tokens. A token is a maximal sequence of consecutive characters that are not delimiters.
  • If returnDelims is true, delimiter characters are themselves considered to be tokens. Thus, a token is either one delimiter character or a maximal sequence of consecutive characters that are not delimiters.

The following program demonstrates it:

Download  Run Code

5. Using String.Split() method

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

Download  Run Code

6. Using Guava Library

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

Download Code

7. Using String.chars() method

Java 8 provides a new method, String.chars(), which returns an IntStream (a stream of ints) representing an integer representation of characters in the String. This method does not return the 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 achieve that, as shown below:

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.

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 the specified string. To find the name of the backing array, we can print all the fields of String class using the following code and search one with the type char[].

Update: This is unsupported after Java 8.

Download Code

That’s all about iterating over characters of a Java String.

 
Related Post:

Iterate over a string backward in Java

Rate this post

Average rating 4.84/5. Vote count: 62

No votes so far! Be the first to rate this post.

We are sorry that this post was not useful for you!

Tell us how we can improve this post?




Thanks for reading.

Please use our online compiler to post code in comments using C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.

Like us? Refer us to your friends and help us grow. Happy coding :)



Subscribe
Notify of
guest
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Do NOT follow this link or you will be banned from the site!