In Java, you can use String.toCharArray() to convert a String into a char array.
StringToCharArray.java
package com.mkyong.utils;
public class StringToCharArray {
public static void main(String[] args) {
String password = "password123";
char[] passwordInCharArray = password.toCharArray();
for (char temp : passwordInCharArray) {
System.out.println(temp);
}
}
}
Output
p
a
s
s
w
o
r
d
1
2
3
Java 8 – Convert String to Stream Char
For Java 8, you can uses .chars()
to get the IntStream
, and convert it to Stream Char via .mapToObj
package com.mkyong.utils;
package com.mkyong.pageview;
public class Test {
public static void main(String[] args) {
String password = "password123";
password.chars() //IntStream
.mapToObj(x -> (char) x)//Stream<Character>
.forEach(System.out::println);
}
}
Output
p
a
s
s
w
o
r
d
1
2
3
References
- JavaDoc – toCharArray
- Why is String.chars() a stream of ints in Java 8?