How to convert a char array back to a string? How to convert a char array back to a string? arrays arrays

How to convert a char array back to a string?


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);

or

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);