Convert char array to String in Java


In this tutorial, we will understand the different ways to convert a char or character array to String in Java. A character array contains a group of characters while a string also contains a set of characters that terminates with ‘\0’

Convert char array to string in java

Methods to Convert char array to String in Java

Below are the different ways to convert a char array to String in Java.

Convert character array to String

Using String constructor

We can convert a char array to String in Java by specifying the character array to the String constructor. The below example shows the conversion of character array to String using the String constructor.

public class CharArrayToString {

  public static void main(String[] args) {
    char[] ch = {'a','r','r','a','y'};
    
    String s = new String(ch);
    System.out.println(s);

  }

}
array

Using StringBuilder

The StringBuilder class contains append() method to convert the character array to String in Java. The append() method appends the individual characters from the array to form a string. Below is an example of using the StringBuilder class to convert a char to a string.

public class CharArrayToString {

  public static void main(String[] args) {
    char[] ch = {'a','r','r','a','y'};
    
    StringBuilder sb = new StringBuilder();
    sb.append(ch);
    
    System.out.println(sb);

  }

}
array

We can also use the toString() method to return the string representation of the characters formed using the append() method.

public class CharArrayToString {

  public static void main(String[] args) {
    char[] ch = {'a','r','r','a','y'};
    
    StringBuilder sb = new StringBuilder();
    sb.append(ch);
    
    String s = sb.toString();
    
    System.out.println(s);

  }

}
array

Using String.valueOf method

The String class contains valueOf() method that converts a character array to its equivalent string representation. This method takes the character array as a parameter and converts it into a string. In the below example, we create a char array with a variable ch and pass that as a parameter to the valueOf() method. This method returns a string strValue that we store as a String variable. To access the valueOf() method, we need to create another string variable s.

public class CharArrayToString {

  public static void main(String[] args) {
    char[] ch = {'a','r','r','a','y'};
    String s = new String();
    String strValue = s.valueOf(ch);
    
    System.out.println(strValue);

  }

}
array

Using copyValueOf method

The copyValueOf() is also a method of the String class and works the same as the valueOf() method. It takes the character array as a parameter and parses it into a String. Below is an example that uses the copyValueOf() method to convert char array to string.

public class CharArrayToString {

  public static void main(String[] args) {
    char[] ch = {'a','r','r','a','y'};
    String s = new String();
    String strValue = s.copyValueOf(ch);
    
    System.out.println(strValue);

  }

}
array

Using streams of Java8

Java 8 supports a new technique to convert char array to string using the Streams concept. We can create a stream over an Array of type T by using the Arrays.stream(T[] object). Using the map function, we can call the valueOf() method that invokes each character in the array and joins them to form a string using the Collectors.joining() method.

import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CharArrayToString {

  public static void main(String[] args) {
    
    Character[] cArray = {'a','r','r','a','y'};
    Stream<Character> cstream = Arrays.stream(cArray);
    String strValue = cstream.map(String::valueOf).collect(Collectors.joining());
    
    System.out.println(strValue);

  }

}
array

You might be also interested in reading

Convert int to String in Java

Convert char to string in Java

Convert a char to int Java

Reference