Java – String to char[] array conversion in 4 ways

In this article, we will discuss various ways to convert String to char[] array in Java

Q) What is the need of converting String to primitive char[] array or Character[] array wrapper-type ?

  • Generally, whenever we receive any data from web application then it is passed in the form of String only
  • To use char[] array in Java application, it needs to be converted into primitive char[] or wrapper-type Character[] array first and then we can use accordingly depending upon business requirements
  • This article explains about String to primitive char[] array or Character[] array wrapper-type conversion only, but we can do conversion for other types like int, double, float, long, boolean, etc
  • Note: Likewise, sometime Character[] array to String conversion is also required

Ways to convert String to Character[] Arrays :

  1. Using toCharArray() method of String class (direct conversion)
  2. Iterate through String using regular forloop and assign character into char[] array after initializing with length of the String
  3. Direct assigning to Character[] using charAt(index) method
  4. Direct conversion using Java 1.8 Stream

Read String class in detail with example

Let us move forward and discuss all possible ways to convert String to Character[] array in Java

1. Using toCharArray() method of String :

  • This method can be used to convert String into char[] array
  • This approach is direct conversion
  • We can use any forloop to iterate through char[] array to print charvalues to console

Method signature:

public char[] toCharArray();

ConvertStringIntoCharacterArrayUsingToCharArray.java

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

import java.util.Arrays;

public class ConvertStringIntoCharacterArrayUsingToCharArray {

	public static void main(String[] args) {

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


		// 2. converting String to char[] array
		char[] chArray = str.toCharArray();


		// 3. print to console
		System.out.print("\nString to char[] Arrays :- \n" 
				+ Arrays.toString(chArray));
	}
}

Output:

Original String :- 
BenchResources

String to char[] Arrays :- 
[B, e, n, c, h, R, e, s, o, u, r, c, e, s]

2. Iterate through String and assign characters to char[] Arrays :

  • First, create primitive char[] arrays using the length of String
  • Then, iterate through String using regular forloop and assign charvalue inside char[] array created in the step-1
  • To get character at each indexposition, use charAt(index); method of String

Method signature:

public char charAt(int index);

ConvertStringIntoCharArrayUsingCharAtMethod.java

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

import java.util.Arrays;

public class ConvertStringIntoCharArrayUsingCharAtMethod {

	public static void main(String[] args) {

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


		// 2. create primitive char[] array of string length
		char[] chArray = new char[str.length()];


		// 3. iterate through char[] array using for-each loop
		for(int index = 0; index < str.length(); index++) {

			// 3.1 add each char to char[] array using index-position
			chArray[index] = str.charAt(index);
		}


		// 4. print to console
		System.out.print("\nString to char[] Arrays :- \n" 
				+ Arrays.toString(chArray));
	}
}

Output:

Original String :- 
BenchResources

String to char[] Arrays :- 
[B, e, n, c, h, R, e, s, o, u, r, c, e, s]

3. Direct assigning to Character[] array using charAt(index) method :

  • First, create wrapper-type Character[] Arrays
  • Then, iterate through String using regular forloop and assign charvalue inside char[] Arrays created in the step-1
  • To get character at each indexposition, use charAt(index); method of String

Method signature:

public char charAt(int index);

ConvertStringIntoCharacterArrayUsingCharAtMethod.java

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

import java.util.Arrays;

public class ConvertStringIntoCharacterArrayUsingCharAtMethod {

	public static void main(String[] args) {

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


		// 2. Create: wrapper-type Character[] array of string length
		Character[] chArray = new Character[str.length()];


		// 3. Add: iterate through char[] array using for-each loop
		for(int index = 0; index < str.length(); index++) {

			// 3.1 add each char to char[] array using index-position
			chArray[index] = str.charAt(index);
		}


		// 4. print to console
		System.out.print("\nString to char[] Arrays :- \n" 
				+ Arrays.toString(chArray));
	}
}

Output:

Original String :- 
BenchResources

String to char[] Arrays :- 
[B, e, n, c, h, R, e, s, o, u, r, c, e, s]

4. Using Java 1.8 Stream :

  • This is very easiest one among various alternatives discussed above
  • Get input stream and map the objects to char values
  • And then finally invoke toArray() method passing new Character object

Method signature:

Character[] chArray = str.chars()
						.mapToObj(ch -> (char)ch)
						.toArray(Character[]::new);

ConvertStringIntoCharacterArrayUsingJava8.java

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

import java.util.Arrays;

public class ConvertStringIntoCharacterArrayUsingJava8 {

	public static void main(String[] args) {

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


		// 2. convert String to char[] Arrays using Java 8 Stream
		Character[] chArray = str.chars()
				.mapToObj(ch -> (char)ch)
				.toArray(Character[]::new); 


		// 3. print to console
		System.out.print("\nString to char[] Arrays :- \n" 
				+ Arrays.toString(chArray));
	}
}

Output:

Original String :- 
BenchResources

String to char[] Arrays :- 
[B, e, n, c, h, R, e, s, o, u, r, c, e, s]

Hope, you found this article very helpful. If you have any suggestion or want to contribute any other way or tricky situation you faced during Interview hours, then share with us. We will include that code here.

Related Articles:

References:

Happy Coding !!
Happy Learning !!

Java - Character[] array to String conversion in 5 ways
Java - Character to String conversion in 6 ways