Convert String to Char Array

Harinath
1,084 views

Open Source Your Knowledge, Become a Contributor

Technology knowledge has to be shared and made accessible for free. Join the movement.

Create Content

Using String.toCharArray()

Use String.toCharArray() to convert a String into a char array

1
6
7
8
9
10
11
12
13
// {...}
String techioStr = "TechIO Playground";
char[] techioCharArray = techioStr.toCharArray();
for (char techioChar : techioCharArray) {
System.out.println(techioChar);
}
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Convert String to Char Array Using Java 8 Stream

Use .chars() to get the IntStream, and convert it to Stream Char using .mapToObj

1
6
7
8
9
10
11
// {...}
String techioStr = "TechIO Playground";
techioStr.chars() //IntStream
.mapToObj(x -> (char) x)//Stream<Character>
.forEach(System.out::println);
//{...}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content