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?
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?
Try this
You can use
String.valueOf
method.For example,
For more on char array to string you can refer links below
https://docs.oracle.com/javase/7/docs/api/java/lang/String.html
https://www.flowerbrackets.com/char-array-to-string-java/
This will convert char array back to string:
1 alternate way is to do:
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
you are essentially telling the compiler to autobox a String object around your array of characters.