xxxxxxxxxx
// Convert a string to a char array
String str = "Hello";
char[] charArray = str.toCharArray();
xxxxxxxxxx
char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
String str = new String(a);
xxxxxxxxxx
final char[] charArray = { 'g','e','e','k','s','f','o','r','g','e','e','k','s' };
String string = new String(charArray);
System.out.println(string);
// -> "geeksforgeeks"
xxxxxxxxxx
String myString = "Hello, world!";
char[] myCharArray = myString.toCharArray();
xxxxxxxxxx
public class Demo {
public static void main(String []args) {
String str = "Tutorial";
System.out.println("String: "+str);
char[] ch = str.toCharArray();
System.out.println("Character Array...");
for (int i = 0; i < ch.length; i++) {
System.out.print(ch[i]+" ");
}
}
}
OUTPUT:
String: Tutorial
Character Array
T u t o r i a l
xxxxxxxxxx
// Method 1: Using String object
char[] ch = {'g', 'o', 'o', 'd', ' ', 'm', 'o', 'r', 'n', 'i', 'n', 'g'};
String str = new String(ch);
System.out.println(str);
xxxxxxxxxx
const string = 'word';
// Option 1
string.split('');
// Option 2
[string];
// Option 3
Array.from(string);
// Option 4
Object.assign([], string);
// Result:
// ['w', 'o', 'r', 'd']
xxxxxxxxxx
String s="abcd";
char[] a=s.toCharArray();
for(char c:a)
{ System.out.println(c); }