How to convert a char array back to a string?

770,552

Solution 1

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.

Solution 2

String text = String.copyValueOf(data);

or

String text = String.valueOf(data);

is arguably better (encapsulates the new String call).

Solution 3

This will convert char array back to string:

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

Solution 4

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

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

Arrays.toString(c);

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

And:

String.valueOf(c)

Returns the string wwwwww3333dfevvv.

In Summary: pay attention to Arrays.toString(c), because you'll get "[w, w, w, w, w, w, 3, 3, 3, 3, d, f, e, v, v, v]" instead of "wwwwww3333dfevvv".

Solution 5

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

char[]

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.

Share:
770,552
chutsu
Author by

chutsu

Updated on July 08, 2022

Comments

  • chutsu
    chutsu almost 2 years

    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?

  • A.H.
    A.H. over 12 years
    Both methods call String(char[]) or a variant of that. And the copy part is done inside String(char[]). This leaves no benefit to a direct call besides symmetry with the other valueOf methods.
  • A.H.
    A.H. over 12 years
    static and more OOP is a contradiction of terms. Anything declared static is not part an object or its behaviour and hence not object oriented. Besides of this - if there would be any chance that the implementation of String will be changed and/or enhanced in an incompatible way or several competing implementations can be chosen at runtime, then a static factory method makes sense. This will not happen with such a low level thing as String. Therefore my premise is: Use the smallest hammer suitable, not the largest one available.
  • corsiKa
    corsiKa about 11 years
    @A.H. I don't know about that. String gave way to StringBuffer which gave way to StringBuilder. I bet StringBuffer proponents said the same thing then, and now have to refactor code to use StringBuilder.
  • A.H.
    A.H. about 11 years
    @corsiKa: StringBuffer might have been superseded by StringBuilder. This is how I understand "give way to". But String has not been superseded by any of them. And never will. The advantage of immutable types (which String effectively is) is to good and to important. Also StringBuilder and -Buffer can coexist quite nicely. Two different String representations are a completely different beast. I have a strong doubt that the existing APIs and language tweaks would benefit from that.
  • corsiKa
    corsiKa about 11 years
    @A.H. The fact that they had to create the CharSequence interface shows how flawed your "wont happen with low level stuff like string" is - they don't change what goes on in string because they tied themselves to it early on, and now they wish they wouldn't have.
  • A.H.
    A.H. about 11 years
    @corsiKa I did not say, that the String API is all good and golden. Only that it won't change in an incompatible way and that the class itself and its constructors won't go anywhere. :-)
  • fommil
    fommil about 10 years
    this is wrong as it assumes that toString works correctly on char[]. It might work on some specific vendors and versions of the JVM.
  • Alpay
    Alpay almost 10 years
    Because String class is immutable, this will cause two operations to be performed: toString() call on a and creation of another String object that concatenates with a.toString() and ""
  • rakensi
    rakensi about 9 years
    In some situations, the method String.valueOf(char[] data, int offset, int count) is useful. One example is the characters() method in the SAX API for processing XML.
  • baikho
    baikho almost 7 years
    While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.
  • Casey
    Casey over 6 years
    Why though? What do you get from doing this?
  • Prasad Karunagoda
    Prasad Karunagoda over 3 years
    Arrays.toString(new char[] {'a', 'b', 'c'}) returns "[a, b, c]"; not "abc".