当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - StringBuilder.Append for string array?

问题描述:

Say I have a StringBuilder object

var sb = new StringBuilder();

And an arbritrary array of strings

var s = new []{"a","b","c"};

Is this the 'quickest' way to insert them into the stringbuilder instance?

sb.Append(string.join(string.empty, s));

Or does StringBuilder have a function I have overlooked?

Edit: Sorry I dont know how many items sb will contain, or how many items may be in each String[].

网友答案:

If you mean by "quickest" most performant than better use:

for(int i = 0; i < myArrayLen; i++)
  sb.Append(myArray[i]);
网友答案:

string.Concat(...) should be faster than string.Join("", ...). Also, this depends on what else you're doing with your StringBuilder. If you're only performing a few concatenations then it can be faster not to use it.

More context always helps!

网友答案:

Believe it or not, but string.Concat is faster than StringBuilder when the strings are 4/5.
This article discuss the question very well.

网友答案:

To do this in one line without a loop, you could do this:

sb.Append(String.Join(Environment.NewLine, s));

and will also work where s is any type of

IEnumerable<string>
网友答案:

I believe you already have the correct answer:

sb.Append(string.join(string.empty, s))
分享给朋友:
您可能感兴趣的文章:
随机阅读: