本文翻译自:How to convert a char to a String?
I have a char
and I need a String
. 我有一个char
,我需要一个String
。 How do I convert from one to the other? 我如何从一个转换为另一个?
参考:https://stackoom.com/question/YI1E/如何将char转换为String
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();
// 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) . 注意: Character.toString(char)返回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. String.valueOf(char[] value)
调用new String(char[] value)
,后者又设置value
char数组。
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.valueOf(char value)
调用以下包私有构造函数。
String(char[] value, boolean share) {
// assert share : "unshared not supported";
this.value = value;
}
Source code from String.java
in Java 8 source code Java 8源代码中 String.java
源代码
Hence
String.valueOf(char)
seems to be most efficient method, in terms of both memory and speed, for convertingchar
toString
. 因此,就内存和速度而言,String.valueOf(char)
似乎是将char
转换为String
最有效方法。
Below are various ways to convert to char c to String s (in decreasing order of speed and efficiency) 下面是转换为char c到String s的各种方法(按速度和效率的降序排列)
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
We have various ways to convert a char
to String
. 我们有各种方法将char
转换为String
。 One way is to make use of static method toString()
in Character
class: 一种方法是在Character
类中使用静态方法toString()
:
char ch = 'I';
String str1 = Character.toString(ch);
Actually this toString
method internally makes use of valueOf
method from String
class which makes use of char array: 实际上这个toString
方法在内部使用了String
类中的valueOf
方法,该方法使用了char数组:
public static String toString(char c) {
return String.valueOf(c);
}
So second way is to use this directly: 所以第二种方法是直接使用它:
String str2 = String.valueOf(ch);
This valueOf
method in String
class makes use of char array: String
类中的valueOf
方法使用char数组:
public static String valueOf(char c) {
char data[] = {c};
return new String(data, true);
}
So the third way is to make use of an anonymous array to wrap a single character and then passing it to String
constructor: 所以第三种方法是使用匿名数组来包装单个字符,然后将其传递给String
构造函数:
String str4 = new String(new char[]{ch});
The fourth way is to make use of concatenation: 第四种方法是使用串联:
String str3 = "" + ch;
This will actually make use of append
method from StringBuilder
class which is actually preferred when we are doing concatenation in a loop. 这实际上将使用StringBuilder
类中的append
方法,当我们在循环中进行连接时,它实际上是首选的。
Here are a few methods, in no particular order: 以下是一些方法,没有特别的顺序:
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();
I've tried the suggestions but ended up implementing it as follows 我已经尝试了这些建议但最终实现如下
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;
}
}
}});