首页 > 编程知识 正文

如何将char转换为String?

时间:2023-05-03 07:01:36 阅读:199147 作者:4774

本文翻译自: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? 我如何从一个转换为另一个?

#1楼

参考:https://stackoom.com/question/YI1E/如何将char转换为String

#2楼

Nice question. 好问题。 I've got of the following five 6 methods to do it. 我有以下 五种 方法可以做到这一点。

1. String stringValueOf = String.valueOf('c'); // most efficient2. String stringValueOfCharArray = String.valueOf(new char[]{x});3. String 高兴的小懒虫 = Character.toString('c');4. String hmddb = 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 converting char to String . 因此,就内存和速度而言, String.valueOf(char)似乎是将char转换为String最有效方法。

How to convert primitive char to String in Java 如何在Java中将原始char转换为String How to convert Char to String in Java with Example 如何使用Example将Java中的Char转换为String #3楼

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 efficientString 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#4楼

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方法,当我们在循环中进行连接时,它实际上是首选的。

#5楼

Here are a few methods, in no particular order: 以下是一些方法,没有特别的顺序:

char c = 'c';String s = Character.toString(c); // Most efficient ways = new Character(c).toString(); // Same as above except new Character objects needs to be garbage-collecteds = c + ""; // Least efficient and most memory-inefficient, but common amongst beginners because of its simplicitys = String.valueOf(c); // Also quite commons = String.format("%c", c); // Not commonFormatter formatter = new Formatter();s = formatter.format("%c", c).toString(); // Same as aboveformatter.close();#6楼

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; } } }});

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。