Convert a char array to a String in Java
This post will discuss how to convert a char array to string in Java.
There are several ways to convert a char array to a String in Java, which is the process of joining the individual characters in the array into a string sequence. Since the string is immutable in Java, the subsequent modification of the character array does not affect the allocated string. Some of the possible methods are:
1. Using String Constructor
The simplest solution is to pass the char array into the String() constructor. It takes a char array as a parameter and creates a new String object that represents the sequence of characters in the array. It internally uses Arrays.copyOf() to copy the contents of the character array.
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { char[] chars = {'T', 'e', 'c', 'h', 'i', 'e'}; String string = new String(chars); System.out.println(string); // "Techie" } } |
2. Using String.valueOf() or String.copyValueOf() method
We can encapsulate the string constructor call by using the String.valueOf() method, which internally does the same thing. The String.valueOf() method is a static method that takes a char array as a parameter and returns a new String object that contains the same characters as the array. We can also use the static method String.copyValueOf() that is similar to the String.valueOf() method.
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { char[] chars = {'T', 'e', 'c', 'h', 'i', 'e'}; String string = String.copyValueOf(chars); System.out.println(string); // "Techie" } } |
3. Using StringBuilder class
Another plausible way of converting a char array to string is using the StringBuilder class. This is a mutable class that can be used to append characters to a string. We can iterate through the char array and append each character to a StringBuilder object. Then, we can use the toString() method of the StringBuilder class to get the final string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Main { public static void main(String[] args) { char[] chars = {'T', 'e', 'c', 'h', 'i', 'e'}; StringBuilder sb = new StringBuilder(); for (char ch: chars) { sb.append(ch); } String string = sb.toString(); System.out.println(string); // "Techie" } } |
4. Using Arrays.toString() method (Not recommended)
The idea here is to get the string representation of the specified array. The string representation consists of a list of the array’s elements, enclosed in square brackets "[]", and all adjacent elements are separated by a comma, followed by a single space ", ". We can easily get rid of the square brackets by calling the substring() method, and comma and space by using the replaceAll() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.util.Arrays; class Main { public static void main(String[] args) { char[] chars = {'T', 'e', 'c', 'h', 'i', 'e'}; String string = Arrays.toString(chars) .substring(1, 3 * chars.length - 1) .replaceAll(", ", ""); System.out.println(string); // "Techie" } } |
That’s all about converting a char array to a String in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)