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

后端 未结 9 942
别那么骄傲
别那么骄傲 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 05:02

    I prefer using String.valueOf for single conversions - but in your case you really want concatenation.

    However, I would suggest that this version would remove all potential ambiguity:

    String s = c1 + "" + c2;
    

    That way there's no possibility, however remote, of someone considering whether c1 and c2 will be added together before the concatenation.

提交回复
热议问题