How to convert a char to a String?
我有一个
您可以使用
正如其他人所指出的,字符串连接也可以作为快捷方式:
1 |
但这可以归结为:
1 |
由于
好问题。我有以下几种方法可以做到。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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(); // Although this method seems very simple, // this is less efficient because the concatenation // expands to new StringBuilder().append(x).append("").toString(); 5. String concatBlankString = 'c' +""; 6. String fromCharArray = new String(new char[]{x}); |
Note: Character.toString(char) returns String.valueOf(char). So effectively both are same.
另一方面,
1 2 3 4 | String(char[] value, boolean share) { // assert share :"unshared not supported"; this.value = value; } |
源代码源于Java 8源代码中的EDOCX1 17
Hence
String.valueOf(char) seems to be most efficient method, in terms of both memory and speed, for convertingchar toString .
以下是将字符C转换为字符串S的各种方法(按速度和效率的降序排列)
1 2 3 4 5 6 7 |
使用以下任何一种:
使用
试试这个:
如@warfox所述-有6种方法可以将char转换为字符串。然而,最快的将是通过串联,尽管上面的答案表明它是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | @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}); } } |
结果:
1 2 3 4 5 6 7 8 | 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 |
如你所见,最快的是
1 | VM version: JDK 1.8.0_131, VM 25.131-b11 |
这种性能差异是由于
以下是一些方法,没有特别的顺序:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | char c = 'c'; String s = Character.toString(c); // Most efficient way s = new Character(c).toString(); // Same as above except new Character objects needs to be garbage-collected s = c +""; // Least efficient and most memory-inefficient, but common amongst beginners because of its simplicity s = String.valueOf(c); // Also quite common s = String.format("%c", c); // Not common Formatter formatter = new Formatter(); s = formatter.format("%c", c).toString(); // Same as above formatter.close(); |
我们有多种方法可以将
实际上,这个
所以第二种方法是直接使用它:
该
因此,第三种方法是使用匿名数组包装单个字符,然后将其传递给
第四种方法是使用串联:
1 |
这实际上将使用来自
我正在将char数组转换为字符串
我已经尝试过这些建议,但最终实现如下
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | editView.setFilters(new InputFilter[]{new InputFilter() { @Override public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { String prefix ="http://"; //make sure our prefix is visible String destination = dest.toString(); //Check If we already have our prefix - make sure it doesn't //get deleted if (destination.startsWith(prefix) && (dstart <= prefix.length() - 1)) { //Yep - our prefix gets modified - try preventing it. int newEnd = (dend >= prefix.length()) ? dend : prefix.length(); SpannableStringBuilder builder = new SpannableStringBuilder( destination.substring(dstart, newEnd)); builder.append(source); if (source instanceof Spanned) { TextUtils.copySpansFrom( (Spanned) source, 0, source.length(), null, builder, newEnd); } return builder; } else { //Accept original replacement (by returning null) return null; } } }}); |