C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
String: To convert a char array to a String, we use the String constructor. An optional start index and count parameter can be used.
ConstructorsHere: We test the result string. We show it is a String that contains the letters "abc" at its start.
Java program that uses char array
public class Program {
public static void main(String[] args) {
// Create a char array of 26 characters.
// ... Add all letters to it.
char[] array = new char[26];
int index = 0;
for (char c = 'a'; c <= 'z'; c++) {
array[index++] = c;
}
String result = new String(array); // Convert to a string.
// ... Display parts of our new string.
System.out.println(result.startsWith("abc"));
System.out.println(result.length());
System.out.println(result);
}
}
Output
true
26
abcdefghijklmnopqrstuvwxyz
Tip: Int values, which represent chars in ASCII, can also be used in a char array initializer.
Java program that uses char array initializer
public class Program {
public static void main(String[] args) {
// Use array initializer syntax.
char[] array = { 'a', 'r', 't', 'i', 's', 't' };
// Add only the first three characters to a new string.
String result = new String(array, 0, 3);
System.out.println(result);
}
}
Output
art
Note: To the compiler, 97 is the same as "a" but using "a" is more readable for humans.
Java program that uses char array, ints
public class Program {
public static void main(String[] args) {
// Numbers can be stored in a char array.
// ... These indicate a char based on ASCII.
char[] values = new char[3];
values[0] = 97;
values[1] = 98;
values[2] = 99;
System.out.println(values);
// We can specify letters as well.
char[] values2 = { 'a', 'b', 'c' };
System.out.println(values2);
}
}
Output
abc
abc
Java program that uses toCharArray
public class Program {
public static void main(String[] args) {
String value = "cat";
// Convert string to a char array.
char[] array = value.toCharArray();
array[0] = 'h';
// Loop over chars in the array.
for (char c : array) {
System.out.println(c);
}
}
}
Output
h
a
t
Version 1: This version of the code uses a char array to build up a buffer of 10 characters. It converts the buffer to a string.
Version 2: This code uses the StringBuilder type to append 10 chars and then calls toString.
StringBuilderResult: If a program creates strings of known length, using a char array is a worthwhile optimization.
Java program that benchmarks char array, StringBuilder
import java.lang.StringBuilder;
public class Program {
public static void main(String[] args) {
long t1 = System.currentTimeMillis();
// Version 1: create string from a char array.
for (int i = 0; i < 1000000; i++) {
char[] array = new char[10];
for (int v = 0; v < 10; v++) {
array[v] = '?';
}
String result = new String(array);
}
long t2 = System.currentTimeMillis();
// Version 2: create string from a StringBuilder.
for (int i = 0; i < 1000000; i++) {
StringBuilder builder = new StringBuilder();
for (int v = 0; v < 10; v++) {
builder.append('?');
}
String result = builder.toString();
}
long t3 = System.currentTimeMillis();
// ... Benchmark timings.
System.out.println(t2 - t1);
System.out.println(t3 - t2);
}
}
Output
38 ms: char[] array
81 ms: StringBuilder append