English 中文(简体)
如何将果园改成 Str?
原标题:How to convert a char to a String?

页: 1 我如何从一个转变到另一个?

问题回答

下面的5> > > > > 6方法。

// Method #1
String stringValueOf = String.valueOf( c ); // most efficient

// Method #2
String stringValueOfCharArray = String.valueOf(new char[]{x});

// Method #3
String characterToString = Character.toString( c );

// Method #4
String characterObjectToString = new Character( c ).toString();

// Method #5
// Although this approach seems very simple, 
// this is less efficient because the concatenation
// expands to a StringBuilder.
String concatBlankString =  c  + "";

// Method #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> 援引new String(char[] Value>,后者反过来又规定了 Value char range。

public String(char value[]) {
    this.value = Arrays.copyOf(value, value.length);
}

另一方面,<代码> String. ValueOf(char Value) 援引了以下 Package private Constructionor.

String(char[] value, boolean share) {
    // assert share : "unshared not supported";
    this.value = value;
}

Source Code from String.java in Java 8 source Code

因此,从记忆和速度来看,将<条码>/条码>改为<条码>。

来源:

  1. How to convert primitive char to String in Java
  2. How to convert Char to String in Java with Example

下面是各种方式,以转换成果园(以降低速度和效率的速度)。

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

使用以下任何手段:

String str = String.valueOf( c );
String str = Character.toString( c );
String str =  c  + "";

Use the Character.toString() method like so:

char mChar =  l ;
String s = Character.toString(mChar);

@WarFox说,有6种方法将果园改道。 然而,最快的办法是通过分类,尽管上文已经回答说它是<代码>。 String. ValueOf。 这里的基准证明:

@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

如您所知,最快的是c + “ or >+ c;

VM version: JDK 1.8.0_131, VM 25.131-b11

这一业绩差异是由于<代码>-XX:+OptimizeStringConcat优化所致。 https://stackoverflow.com/questions/42193955/why-is-string-concatenation-faster-than-string- Valueof-for-verting-an-integer>here

引申:Character.toString(aChar) or Just this:aChar + “

我们有各种办法将<条码>代谢/代码>改为<条码>。 <>One way is to make use of一个静止方法toString() in Character 班级:

char ch =  I ; 
String str1 = Character.toString(ch);

实际使用<代码>至String方法 利用果园阵列的方法:

public static String toString(char c) {
    return String.valueOf(c);
}

So second way is to use this directly:

String str2 = String.valueOf(ch);

* E/CN.6/2009/1。 缩略语

public static String valueOf(char c) {
        char data[] = {c};
        return new String(data, true);
}

因此,third >方法是使用匿名阵列,以包罗单一特性,然后将其通过至String 构造者:

String str4 = new String(new char[]{ch});

<>四><>>

String str3 = "" + ch;

这将实际上利用<代码>应用。 <代码>StringBuilder 类别,在我们进行整 lo时实际上更可取。

这里有几种方法,特别是:

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 am converting Char Array to String

Char[] CharArray={  A ,  B ,  C };
String text = String.copyValueOf(CharArray);

我研究了这些建议,但最后执行如下。

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

将<代码>char/code>改为String。 String. ValueOf methods.

To convert a String to a char, you can use the String.charAt method.

char character =  a ;
String string = String.valueOf(character);
String string = "a";
char character = string.charAt(0);




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签