Convert String to Char Array
Harinath
16.3K views
Using String.toCharArray()
Use String.toCharArray() to convert a String into a char array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// {
public class Main {
public static void main(String[] args) {
// }
String techioStr = "TechIO Playground";
char[] techioCharArray = techioStr.toCharArray();
for (char techioChar : techioCharArray) {
System.out.println(techioChar);
}
//{
}
}
//}
Convert String to Char Array Using Java 8 Stream
Use .chars() to get the IntStream, and convert it to Stream Char using .mapToObj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// {
public class Main {
public static void main(String[] args) {
// }
String techioStr = "TechIO Playground";
techioStr.chars() //IntStream
.mapToObj(x -> (char) x)//Stream<Character>
.forEach(System.out::println);
//{
}
}
//}
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.