How to convert a char array back to a string?
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 char
s and join them back is not required in normal code.
Compare this to C/C++ where "foo"
you have a bundle of char
s 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.
chutsu
Updated on July 08, 2022Comments
-
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. over 12 yearsBoth methods call
String(char[])
or a variant of that. And the copy part is done insideString(char[])
. This leaves no benefit to a direct call besides symmetry with the othervalueOf
methods. -
A.H. over 12 yearsstatic 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 asString
. Therefore my premise is: Use the smallest hammer suitable, not the largest one available. -
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. 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 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. 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 about 10 yearsthis is wrong as it assumes that
toString
works correctly onchar[]
. It might work on some specific vendors and versions of the JVM. -
Alpay almost 10 yearsBecause String class is immutable, this will cause two operations to be performed: toString() call on
a
and creation of another String object that concatenates witha.toString()
and""
-
rakensi about 9 yearsIn some situations, the method
String.valueOf(char[] data, int offset, int count)
is useful. One example is thecharacters()
method in the SAX API for processing XML. -
baikho almost 7 yearsWhile 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 over 6 yearsWhy though? What do you get from doing this?
-
Prasad Karunagoda over 3 years
Arrays.toString(new char[] {'a', 'b', 'c'})
returns "[a, b, c]"; not "abc".