Is concatenating with an empty string to do a string conversion really that bad?

后端 未结 9 957
别那么骄傲
别那么骄傲 2020-12-03 04:45

Let\'s say I have two char variables, and later on I want to concatenate them into a string. This is how I would do it:

char c1, c2;
// ...

Str         


        
9条回答
  •  盖世英雄少女心
    2020-12-03 04:50

    What about

    new String(new char[] {a, b})
    

    and if you do it alot you could create a class "Strings" with:

    public static String valueOf(char... chars) {
        return new String(chars);
    }
    

    Your line would then read

    String s = Strings.valueOf(a, b);
    

    Nice and short.

    Edited

    A better name might be:

    String s = Chars.asString(a, b);
    

提交回复
热议问题