Is there a way to concatenate char
to form a String
in Java?
Example:
String str;
Char a, b, c;
a = 'i';
b = 'c';
c = 'e';
str = a + b + c; // thus str = "ice";
Is there a way to concatenate char
to form a String
in Java?
Example:
String str;
Char a, b, c;
a = 'i';
b = 'c';
c = 'e';
str = a + b + c; // thus str = "ice";
You can use StringBuilder:
Or if you already have the characters, you can pass a character array to the String constructor:
Try this:
Output:
Use
StringBuilder
:One-liner:
Doing
""+a+b+c
gives:I asked some time ago related question.
Use
str = ""+a+b+c;
Here the first
+
isString
concat, so the result will be aString
. Note where the""
lies is important.Or (maybe) better, use a
StringBuilder
.Use the
Character.toString(char)
method.