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

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

 

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

 

Various ways to convert String to Character[] array

  1. using toCharArray() method of String class (direct conversion)
  2. Iterate through String using regular for-loop 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 version

Read String class in detail with example

 

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

Way 1: using toCharArray() method of String class (direct conversion to char[] array)

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

Method signature:

public char[] toCharArray();

ConvertStringIntoCharacterArrayUsingToCharArray.java

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

public class ConvertStringIntoCharacterArrayUsingToCharArray {

	public static void main(String[] args) {

		// String
		String str1 = "BenchResources";

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

		// Iterate through char[] array using for-each loop
		for(char ch : chArray){
			System.out.println(ch);
		}
	}
}

Output:

B
e
n
c
h
R
e
s
o
u
r
c
e
s

 

Way 2: Iterate through String using regular for-loop and assign character into char[] array after initializing with length of the String

  • First, create primitive char[] array using the length of string
  • Then, iterate through string using regular for-loop and assign char-value inside char[] array created in the step-1
  • To get character at each index-position, use charAt(index); method of String class

Method signature:

public char charAt(int index);

ConvertStringIntoCharArrayUsingCharAtMethod.java

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

public class ConvertStringIntoCharArrayUsingCharAtMethod {

	public static void main(String[] args) {

		// String
		String str = "BenchResources";

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

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

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

		// Print: iterate through char[] array using for-each loop
		for(char ch : chArray){
			System.out.println(ch);
		}
	}
}

Output:

B
e
n
c
h
R
e
s
o
u
r
c
e
s

 

Way 3: direct assigning to Character[] array using charAt(index) method

  • First, create wrapper-type Character[] array
  • Then, iterate through string using regular for-loop and assign char-value inside char[] array created in the step-1
  • To get character at each index-position, use charAt(index); method of String class

Method signature:

public char charAt(int index);

ConvertStringIntoCharacterArrayUsingCharAtMethod.java

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

public class ConvertStringIntoCharacterArrayUsingCharAtMethod {

	public static void main(String[] args) {

		// String
		String str = "BenchResources";

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

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

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

		// Print: iterate through char[] array using for-each loop
		for(char ch : chArray){
			System.out.println(ch);
		}
	}
}

Output:

B
e
n
c
h
R
e
s
o
u
r
c
e
s

 

Way 4: using Java 1.8 version

  • This is very easiest one among various alternatives discussed
  • 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;

public class ConvertStringIntoCharacterArrayUsingJava8 {

	public static void main(String[] args) {

		// String
		String str = "BenchResources";

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

		// Iterate through char[] array using for-each loop
		for(char ch : chArray){
			System.out.println(ch);
		}
	}
}

Output:

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.

 

References:

 

Happy Coding !!
Happy Learning !!

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