Convert a character array to a String in Java
This post will discuss how to convert a primitive character array to a string in Java.
Converting a character array to a string can be useful for manipulating text data or displaying it to the user. There are several ways to convert a character array to a string in Java:
1. Using String Constructor
The String class contains several overloaded versions of its constructor. One such constructor is String(char[]) that accepts a character array as an argument and allocates a new string representing the same sequence of characters as contained in the character array.
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { char[] charArray = { 'J', 'a', 'v', 'a' }; String str = new String(charArray); System.out.println(str); // Java } } |
2. Using String.valueOf() method
The valueOf() method is a static method of the String class that also takes a character array as a parameter and returns a string that contains the same characters as the character array argument. For example:
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { char[] charArray = { 'J', 'a', 'v', 'a' }; String str = String.valueOf(charArray); System.out.println(str); // Java } } |
3. Using String.copyValueOf() method
The copyValueOf() method is similar to the valueOf() method, but it allows specifying the offset and length of the character array to be copied. If copyValueOf() method is called with a single argument, it returns a string that contains all the characters of the specified character array. For example:
|
1 2 3 4 5 6 7 8 9 10 |
class Main { public static void main(String[] args) { char[] charArray = { 'J', 'a', 'v', 'a' }; String str = String.copyValueOf(charArray); System.out.println(str); // Java } } |
4. Using Streams API
This uses a functional programming approach that can process elements of a collection in parallel. It converts the character array to a stream of characters and then collects them into a string using the joining collector. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import java.util.stream.Collectors; import java.util.stream.Stream; class Main { public static void main(String[] args) { char[] charArray = { 'J', 'a', 'v', 'a' }; String string = Stream.of(charArray) .map(String::new) .collect(Collectors.joining()); System.out.println(string); // Java } } |
5. Using StringBuilder class
Finally, we can use a mutable class that can append characters to a string. We can iterate through the character array and appends each character to the StringBuilder instance. Then, we can use the toString() method of the StringBuilder class to get the final string. For example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
class Main { public static void main(String[] args) { char[] charArray = { 'J', 'a', 'v', 'a' }; StringBuilder sb = new StringBuilder(); for (char c: charArray) { sb.append(c); } String str = sb.toString(); System.out.println(str); // Java } } |
That’s all about converting a character array to a String in Java.
Related Post:
Reference: String Javadoc SE 21
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 :)