How to convert a char to a String?
How to convert a char to a String?
Question
I have a char
and I need a String
. How do I convert from one to the other?
Popular Answer
You can use Character.toString(char)
. Note that this method simply returns a call to String.valueOf(char)
, which also works.
As others have noted, string concatenation works as a shortcut as well:
String s = "" + 's';
But this compiles down to:
String s = new StringBuilder().append("").append('s').toString();
which is less efficient because the StringBuilder
is backed by a char[]
(over-allocated by StringBuilder()
to 16
), only for that array to be defensively copied by the resulting String
.
String.valueOf(char)
"gets in the back door" by wrapping the char
in a single-element array and passing it to the package private constructor String(char[], boolean)
, which avoids the array copy.
Read more… Read less…
Nice question. I've got of the following five 6 methods to do it.
// #1
String stringValueOf = String.valueOf('c'); // most efficient
// #2
String stringValueOfCharArray = String.valueOf(new char[]{x});
// #3
String characterToString = Character.toString('c');
// #4
String characterObjectToString = new Character('c').toString();
// #5
// Although this method seems very simple,
// this is less efficient because the concatenation
// expands to new StringBuilder().append(x).append("").toString();
String concatBlankString = 'c' + "";
// #6
String fromCharArray = new String(new char[]{x});
Note: Character.toString(char) returns String.valueOf(char). So effectively both are same.
String.valueOf(char[] value)
invokes new String(char[] value)
, which in turn sets the value
char array.
public String(char value[]) {
this.value = Arrays.copyOf(value, value.length);
}
On the other hand String.valueOf(char value)
invokes the following package private constructor.
String(char[] value, boolean share) {
// assert share : "unshared not supported";
this.value = value;
}
Source code from String.java
in Java 8 source code
Hence
String.valueOf(char)
seems to be most efficient method, in terms of both memory and speed, for convertingchar
toString
.
Below are various ways to convert to char c to String s (in decreasing order of speed and efficiency)
char c = 'a';
String s = String.valueOf(c); // fastest + memory efficient
String s = Character.toString(c);
String s = new String(new char[]{c});
String s = String.valueOf(new char[]{c});
String s = new Character(c).toString();
String s = "" + c; // slowest + memory inefficient
Use the Character.toString()
method like so:
char mChar = 'l';
String s = Character.toString(mChar);
Use any of the following:
String str = String.valueOf('c');
String str = Character.toString('c');
String str = 'c' + "";
As @WarFox stated - there are 6 methods to convert char to string. However, the fastest one would be via concatenation, despite answers above stating that it is String.valueOf
. Here is benchmark that proves that:
@BenchmarkMode(Mode.Throughput)
@Fork(1)
@State(Scope.Thread)
@Warmup(iterations = 10, time = 1, batchSize = 1000, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, batchSize = 1000, timeUnit = TimeUnit.SECONDS)
public class CharToStringConversion {
private char c = 'c';
@Benchmark
public String stringValueOf() {
return String.valueOf(c);
}
@Benchmark
public String stringValueOfCharArray() {
return String.valueOf(new char[]{c});
}
@Benchmark
public String characterToString() {
return Character.toString(c);
}
@Benchmark
public String characterObjectToString() {
return new Character(c).toString();
}
@Benchmark
public String concatBlankStringPre() {
return c + "";
}
@Benchmark
public String concatBlankStringPost() {
return "" + c;
}
@Benchmark
public String fromCharArray() {
return new String(new char[]{c});
}
}
And result:
Benchmark Mode Cnt Score Error Units
CharToStringConversion.characterObjectToString thrpt 10 82132.021 ± 6841.497 ops/s
CharToStringConversion.characterToString thrpt 10 118232.069 ± 8242.847 ops/s
CharToStringConversion.concatBlankStringPost thrpt 10 136960.733 ± 9779.938 ops/s
CharToStringConversion.concatBlankStringPre thrpt 10 137244.446 ± 9113.373 ops/s
CharToStringConversion.fromCharArray thrpt 10 85464.842 ± 3127.211 ops/s
CharToStringConversion.stringValueOf thrpt 10 119281.976 ± 7053.832 ops/s
CharToStringConversion.stringValueOfCharArray thrpt 10 86563.837 ± 6436.527 ops/s
As you can see, the fastest one would be c + ""
or "" + c
;
VM version: JDK 1.8.0_131, VM 25.131-b11
This performance difference is due to -XX:+OptimizeStringConcat
optimization. You can read about it here.