How to convert a char array back to a string?

I have a char array:

char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};

My current solution is to do

String b = new String(a);

But surely there is a better way of doing this?


No, that solution is absolutely correct and very minimal.

Note however, that this is a very unusual situation: Because String is handled specially in Java, even "foo" is actually a String. So the need for splitting a String into individual chars and join them back is not required in normal code.

Compare this to C/C++ where "foo" you have a bundle of chars terminated by a zero byte on one side and string on the other side and many conversions between them due do legacy methods.

String text = String.copyValueOf(data);


String text = String.valueOf(data);

is arguably better (encapsulates the new String call).

This will convert char array back to string:

char[] charArray = {'a', 'b', 'c'};
String str = String.valueOf(charArray);

String str = "wwwwww3333dfevvv";
char[] c = str.toCharArray();

Now to convert character array into String , there are two ways.


Returns the string [w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v].



Returns the string wwwwww3333dfevvv.

A String in java is merely an object around an array of chars. Hence a


is identical to an unboxed String with the same characters. By creating a new String from your array of characters

new String(char[])

you are essentially telling the compiler to autobox a String object around your array of characters.


public class TestDoubleString {

    public static void main(String args[]){
        String str="abbcccddef";    
        char charArray[]=str.toCharArray();
        int len=charArray.length;

        for(int i=0;i<len;i++){
            //if i th one and i+1 th character are same then update the charArray
                catch(Exception e){
        }//finally printing final character string
        for(int k=0;k<charArray.length;k++){
            }       }

You can use String.valueOf method.

For example,

char[] a = {'h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd'};
String b = String.valueOf(a);
System.out.println("Char Array back to String is: " + b);

For more on char array to string you can refer links below

Try this


Try this:

CharSequence[] charArray = {"a","b","c"};

for (int i = 0; i < charArray.length; i++){
    String str = charArray.toString().join("", charArray[i]);

You can also use StringBuilder class

String b = new StringBuilder(a).toString();

Use of String or StringBuilder varies with your method requirements.

1 alternate way is to do:

String b = a + "";

Need Your Help

Escape sequence for ? in c++

c++ string escaping trigraphs

I was looking at the escape sequences for characters in strings in c++ and I noticed there is an escape sequence for a question mark. Can someone tell me why this is? It just seems a little odd a...

Easiest way to extract the urls from an html page using sed or awk only

html regex bash sed awk

I want to extract the URL from within the anchor tags of an html file.