
Baeldung Pro comes with both absolutely No-Ads as well as finally with Dark Mode, for a clean learning experience:
Once the early-adopter seats are all used, the price will go up and stay at $33/year.
Last updated: January 8, 2024
In this quick tutorial, we’ll cover various ways to convert a character array to a String in Java.
The String class has a constructor that accepts a char array as an argument:
@Test
public void whenStringConstructor_thenOK() {
final char[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
String string = new String(charArray);
assertThat(string, is("baeldung"));
}
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.
And speaking of valueOf(), we can even use it directly:
@Test
public void whenStringValueOf_thenOK() {
final char[] charArray = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
String string = String.valueOf(charArray);
assertThat(string, is("baeldung"));
}
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.
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 = { { 'b', 'a' }, { 'e', 'l', 'd', 'u' }, { 'n', 'g' } };
StringBuilder sb = new StringBuilder();
for (char[] subArray : arrayOfCharArray) {
sb.append(subArray);
}
assertThat(sb.toString(), is("baeldung"));
}
We can further optimize the above code by instantiating the StringBuilder of the exact length we need.
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 = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
Stream<Character> charStream = Arrays.stream(charArray);
String string = charStream.map(String::valueOf).collect(Collectors.joining());
assertThat(string, is("baeldung"));
}
The caveat with this approach is that we are invoking the valueOf() over each Character element and so it’ll be pretty slow.
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 = { 'b', 'a', 'e', 'l', 'd', 'u', 'n', 'g' };
String string = Joiner.on("|").join(charArray);
assertThat(string, is("b|a|e|l|d|u|n|g"));
}
Again, note that the join() method will only accept a Character array and not the primitive char array.
In this tutorial, we explored ways of converting a given character array to its String representation in Java.