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 :
- Using toCharArray() method of String class (direct conversion)
- Iterate through String using regular for–loop and assign character into char[] array after initializing with length of the String
- Direct assigning to Character[] using charAt(index) method
- 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 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;
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 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
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 for–loop and assign char–value inside char[] Arrays created in the step-1
- To get character at each index–position, 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:
- Java – String to int conversion – 3 ways
- Java – Integer to String conversion – 6 ways
- Java – String to float conversion – 3 ways
- Java – Float to String conversion – 6 ways
- Java – String to double conversion – 3 ways
- Java – Double to String conversion – 6 ways
- Java – String to long conversion – 3 ways
- Java – Long to String conversion – 6 ways
- Java – String to boolean conversion – 3 ways
- Java – Boolean to String conversion – 6 ways
- Java – String to char conversion
- Java – Character to String conversion – 6 ways
- Java – String to char[] array conversion – 4 ways
- Java – Character[] array to String conversion – 5 ways
- Java – String to byte conversion – 3 ways
- Java – Byte to String conversion – 5 ways
- Java – String to byte[] array conversion
- Java – Byte[] array to String conversion
- Java – String to short conversion – 3 ways
- Java – Short to String conversion – 5 ways
- Java – StringBuffer to String conversion and vice-versa
- Java – StringBuilder to String conversion and vice-versa
- Java – String to Date conversion
- Java – Date to String conversion
References:
- https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
- https://docs.oracle.com/javase/tutorial/java/data/numberclasses.html
- https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html
- https://docs.oracle.com/javase/tutorial/java/data/converting.html
- https://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
- https://docs.oracle.com/javase/tutorial/java/data/strings.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
- https://docs.oracle.com/javase/8/docs/api/java/lang/class-use/String.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html
- https://docs.oracle.com/javase/tutorial/essential/concurrency/imstrat.html
Happy Coding !!
Happy Learning !!