Convert Character Array to String in Java

1. Overview

In this quick tutorial, we’ll cover various ways to convert a character array to a String in Java.

2. String Constructor

The String class has a constructor that accepts a char array as an argument:

@Test 
public void whenStringConstructor_thenOK() {
    final char[] charArray = { 'm', 'a', 'i', 'x', 'u', 'a', 'n', 'v', 'i', 'e', 't' };
    String string = new String(charArray);
    assertThat(string, is("maixuanviet"));
}

This is one of the easiest ways of converting a char array to a String. It internally invokes String#valueOf to create a String object.

3. String.valueOf()

And speaking of valueOf(), we can even use it directly:

@Test
public void whenStringValueOf_thenOK() {
    final char[] charArray = { 'm', 'a', 'i', 'x', 'u', 'a', 'n', 'v', 'i', 'e', 't' };
    String string = String.valueOf(charArray);
    assertThat(string, is("maixuanviet"));
}

String#copyValueOf is another method that’s semantically equivalent to the valueOf() method but was of any significance only in a first few Java releases. As of today, the copyValueOf() method is redundant and we don’t recommend using it.

4. StringBuilder‘s toString()

What if we want to form a String from an array of char arrays?

Then, we can first instantiate a StringBuilder instance and use its append(char[]) method to append all contents together.

Later, we’ll use the toString() method to get its String representation:

@Test
public void whenStringBuilder_thenOK() {
    final char[][] arrayOfCharArray = { { 'm', 'a' }, { 'i, 'x', 'u', 'a' }, { 'n', 'v' }, { 'i', 'e' }, { 't', '.' } };    
    StringBuilder sb = new StringBuilder();
    for (char[] subArray : arrayOfCharArray) {
        sb.append(subArray);
    }
    assertThat(sb.toString(), is("maixuanviet."));
}

We can further optimize the above code by instantiating the StringBuilder of the exact length we need.

5. Java 8 Streams

With Arrays.stream(T[] object) method, we can open a stream over an array of type T.

Considering we have a Character array, we can use the Collectors.joining() operation to form a String instance:

@Test
public void whenStreamCollectors_thenOK() {
    final Character[] charArray = { 'm', 'a', 'i', 'x', 'u', 'a', 'n', 'v', 'i', 'e', 't' };
    Stream<Character> charStream = Arrays.stream(charArray);
    String string = charStream.map(String::valueOf).collect(Collectors.joining());
    assertThat(string, is("maixuanviet"));
}

The caveat with this approach is that we are invoking the valueOf() over each Character element and so it’ll be pretty slow.

6. Guava Common Base Joiner

Let’s say though that the string we need to create is a delimited string. Guava gives us a handy method:

@Test
public void whenGuavaCommonBaseJoiners_thenOK() {
    final Character[] charArray = { 'm', 'a', 'i', 'x', 'u', 'a', 'n', 'v', 'i', 'e', 't' };
    String string = Joiner.on("|").join(charArray);
    assertThat(string, is("m|a|i|x|u|a|n|v|i|e|t"));
}

Again, note that the join() method will only accept a Character array and not the primitive char array.

7. Conclusion

In this tutorial, we explored ways of converting a given character array to its String representation in Java.

As usual, all code examples can be found over on GitHub.