Convert Character Array to String in Java – 在Java中转换字符数组为字符串

最后修改: 2019年 9月 6日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

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

在这个快速教程中,我们将介绍在Java中把一个字符数组转换为String的各种方法。

2. String Constructor

2.字符串构造器

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

String类有一个构造函数,接受一个char数组作为参数:

@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.

这是将char数组转换为String的最简单方法之一。它在内部调用 String#valueOf来创建一个String对象。

3. String.valueOf()

3.String.valueOf()

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

而说到valueOf(),我们甚至可以直接使用它。

@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.

String#copyValueOf是另一个方法,它在语义上等同于valueOf()方法,但只在最初的几个Java版本中有任何意义。到今天为止,copyValueOf()方法是多余的,我们不建议使用它。

4. StringBuilder‘s toString()

4.StringBuildertoString()

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

如果我们想从一个char数组中形成一个String怎么办?

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

然后,我们可以首先实例化一个StringBuilder实例,并使用其append(char[])方法将所有内容附加到一起。

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

稍后,我们将使用 toString()方法来获得其String表示。

@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.

我们可以通过实例化我们需要的确切长度的StringBuilder来进一步优化上述代码。

5. Java 8 Streams

5.Java 8流

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

通过Arrays.stream(T[] object)方法,我们可以在一个T类型的数组上打开一个流。

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

考虑到我们有一个字符数组,我们可以使用Collectors.join()操作来形成一个String实例:

@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.

这种方法的注意事项是,我们在每个字符元素上调用valueOf(),所以会很慢。

6. Guava Common Base Joiner

6.Guava共同基础Joiner

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

比方说,我们需要创建的字符串是一个带分隔符的字符串。Guava给了我们一个方便的方法。

@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.

再次注意,join()方法只接受字符数组,而不是原始的char数组。

7. Conclusion

7.结语

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

在本教程中,我们探讨了在Java中把一个给定的字符数组转换为其String表示的方法。

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

像往常一样,所有的代码例子都可以在GitHub上找到