Java 8 – How to Iterate over char[] Arrays ?

In this article, we will learn how to iterate over char[] Arrays in different ways

Iterate over char[] Arrays :

  • Using Java 8 Stream.chars() method
  • Using Java 8 Stream.codePoints() method
  • Using String.toCharArray() method
  • Using String.split() method
  • Using regular forloop
  • Using StringTokenizer

1. Using Java 8 Stream.chars() method :

  • String.chars() method returns IntStream which can be iterated to get each character of a String using Stream.forEach() method
  • IntStream contains ASCII values of each character which needs to be converted to character by casting

IterateCharactersOfString1.java

package in.bench.resources.string.to.character.array.conversion;

public class IterateCharactersOfString1 {

	public static void main(String[] args) {

		// 1. test String
		String str = "BenchResources.Net";
		System.out.println("Original String :- \n" + str);


		// 2. iterate using Java 8 Stream
		System.out.println("\nIterating over Characters of String :- ");
		str
		.chars()
		.forEach(ch -> System.out.print((char)ch + " "));
	}
}

Output:

Original String :- 
BenchResources.Net

Iterating over Characters of String :- 
B e n c h R e s o u r c e s . N e t 

2. Using Java 8 Stream.codePoints() method :

  • String.codePoints() method returns IntStream which can be iterated to get each character of a String using Stream.forEach() method
  • IntStream contains ASCII values of each character which needs to be converted to character by casting

IterateCharactersOfString2.java

package in.bench.resources.string.to.character.array.conversion;

public class IterateCharactersOfString2 {

	public static void main(String[] args) {

		// 1. test String
		String str = "BenchResources.Net";
		System.out.println("Original String :- \n" + str);


		// 2. iterate using Java 8 Stream
		System.out.println("\nIterating over Characters of String :- ");
		str
		.codePoints()
		.forEach(ch -> System.out.print((char)ch + " "));
	}
}

Output:

Original String :- 
BenchResources.Net

Iterating over Characters of String :- 
B e n c h R e s o u r c e s . N e t 

3. Using String.toCharArray() method :

  • String.toCharArray() method returns char[] Arrays which can be iterated to get each character of a String using regular forloop

IterateCharactersOfString3.java

package in.bench.resources.string.to.character.array.conversion;

public class IterateCharactersOfString3 {

	public static void main(String[] args) {

		// 1. test String
		String str = "BenchResources.Net";
		System.out.println("Original String :- \n" + str);


		// 2. convert String into char[] Arrays
		char[] chArrays = str.toCharArray();


		// 3. iterate using for-loop
		System.out.println("\nIterating over Characters of String :- ");
		for(char ch : chArrays) {
			System.out.print(ch + " ");
		}
	}
}

Output:

Original String :- 
BenchResources.Net

Iterating over Characters of String :- 
B e n c h R e s o u r c e s . N e t 

4. Using String.split() method :

  • String.split() method splits the invoking String based on passed delimiter like space, comma, etc., and returns String[] Arrays
  • In this illustration, empty string (“”) is used to split the invoking String which returns String[] Arrays that can be iterated to get each characters in the form of String as shown below

IterateCharactersOfString4.java

package in.bench.resources.string.to.character.array.conversion;

public class IterateCharactersOfString4 {

	public static void main(String[] args) {

		// 1. test String
		String str = "BenchResources.Net";
		System.out.println("Original String :- \n" + str);


		// 2. convert String into String[] Arrays
		String[] strArrays = str.split("");


		// 3. iterate using for-loop
		System.out.println("\nIterating over Characters of String :- ");
		for(String s : strArrays) {
			System.out.print(s + " ");
		}
	}
}

Output:

Original String :- 
BenchResources.Net

Iterating over Characters of String :- 
B e n c h R e s o u r c e s . N e t 

5. Using regular for-loop :

  • String.charAt() method returns character at the specified indexposition
  • String.length() method provides length of the String which can used to iterate the given String from index0 till String‘s length
  • Using regular forloop, iterate the given String character-by-character as shown in the below illustration

IterateCharactersOfString5.java

package in.bench.resources.string.to.character.array.conversion;

public class IterateCharactersOfString5 {

	public static void main(String[] args) {

		// 1. test String
		String str = "BenchResources.Net";
		System.out.println("Original String :- \n" + str);


		// 2. iterate using for-loop
		System.out.println("\nIterating over Characters of String :- ");
		for(int index=0; index<str.length(); index++) {
			System.out.print(str.charAt(index) + " ");
		}
	}
}

Output:

Original String :- 
BenchResources.Net

Iterating over Characters of String :- 
B e n c h R e s o u r c e s . N e t 

6. Using StringTokenizer :

  • StringTokenizer splits the given String based on the delimiter as shown in the below illustration

IterateCharactersOfString6.java

package in.bench.resources.string.to.character.array.conversion;

import java.util.StringTokenizer;

public class IterateCharactersOfString6 {

	public static void main(String[] args) {

		// 1. test String
		String str = "BenchResources.Net";
		System.out.println("Original String :- \n" + str);


		// 2. create StringTokenizer
		StringTokenizer tokens = new StringTokenizer(str, str, true);


		// 3. iterate using while-loop
		System.out.println("\nIterating over Characters of String :- ");
		while(tokens.hasMoreTokens()) {
			System.out.print(tokens.nextToken() + " ");
		}
	}
}

Output:

Original String :- 
BenchResources.Net

Iterating over Characters of String :- 
B e n c h R e s o u r c e s . N e t 

Related Articles :

References :

Happy Coding !!
Happy Learning !!

Java – How to find first and last character in a String ?
Java 8 – How to convert Celsius to Fahrenheit and vice-versa ?