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
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
// Convert a string to a char array
String str = "Hello";
char[] charArray = str.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
// Convert char array to String in Java
class Util
{
public static void main(String[] args)
{
char[] chars = {'T', 'e', 'c', 'h', 'i', 'e', ' ',
'D', 'e', 'l', 'i', 'g', 'h', 't'};
String string = new String(chars);
System.out.println(string);
}
}
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
char arr[ ] = "This is a test";
string str(arr);
// You can also assign directly to a string.
str = "This is another string";
// or
str = arr;