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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, that solution is absolutely correct and very minimal.
Note however, that this is a very unusual situation: Because
Stringis handled specially in Java, even"foo"is actually aString. So the need for splitting a String into individualchars and join them back is not required in normal code.Compare this to C/C++ where
"foo"you have a bundle ofchars terminated by a zero byte on one side andstringon the other side and many conversions between them due do legacy methods.