How should I concatenate strings?

前端 未结 9 1429
不知归路
不知归路 2020-12-06 00:43

Are there differences between these examples? Which should I use in which case?

var str1 = \"abc\" + dynamicString + dynamicString2;

var str2 = String.Form         


        
9条回答
  •  旧巷少年郎
    2020-12-06 01:07

    As long as you are not deailing with very many (100+) strings or with very large (Length > 10000) strings, the only criterion is readability.

    For problems of this size, use the +. That + overload was added to the string class for readability.

    Use string.Format() for more complicated compositions and when substitutions or formatting are required.

    Use a StringBuilder when combining many pieces (hundreds or more) or very large pieces (length >> 1000). StringBuilder has no readability features, it's just there for performance.

提交回复
热议问题