C# 将字符串列表转换为单个连接字符串的最快方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1128548/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest way to convert a list of strings into a single concatenated string?
提问by jasonh
I have some LINQ code that generates a list of strings, like this:
我有一些生成字符串列表的 LINQ 代码,如下所示:
var data = from a in someOtherList
orderby a
select FunctionThatReturnsString(a);
How do I convert that list of strings into one big concatenated string? Let's say that data has these entries:
如何将该字符串列表转换为一个大的串联字符串?假设数据具有以下条目:
"Some "
"resulting "
"data here."
I should end up with one string that looks like this:
我应该得到一个看起来像这样的字符串:
"Some resulting data here."
How can I do this quickly? I thought about this:
我怎样才能快速做到这一点?我想过这个:
StringBuilder sb = new StringBuilder();
data.ToList().ForEach(s => sb.Append(s));
string result = sb.ToString();
But that just doesn't seem right. If it is the right solution, how would I go about turning this into an extension method?
但这似乎不太对。如果这是正确的解决方案,我将如何将其转变为扩展方法?
采纳答案by Marc Gravell
How about:
怎么样:
public static string Concat(this IEnumerable<string> source) {
StringBuilder sb = new StringBuilder();
foreach(string s in source) {
sb.Append(s);
}
return sb.ToString();
}
and:
和:
string s = data.Concat();
This then has no need for the extra ToList()
/ ToArray()
step.
这样就不需要额外的ToList()
/ToArray()
步骤了。
回答by JaredPar
Have you tried String.Join? If you're already willing to take the overhead of a .ToList call then instead use .ToArray() and combine it with a call to String.Join.
你试过 String.Join 吗?如果您已经愿意承担 .ToList 调用的开销,那么请改用 .ToArray() 并将其与对 String.Join 的调用结合起来。
var joined = String.Concat(someQuery.ToArray());
Note: My solution is likely not the fastest as it involves a bit of overhead in the array. My suspicion is that it would be faster to go more Marc's route. But in most cases if you're just looking for the quick and dirty way to do it in code, my route will work.
注意:我的解决方案可能不是最快的,因为它涉及阵列中的一些开销。我怀疑走更多 Marc 的路线会更快。但是在大多数情况下,如果您只是在寻找在代码中快速而肮脏的方法,我的路线将起作用。
回答by Mike
Use "Aggregate" like this:
像这样使用“聚合”:
List<string> strings = new List<string>() {"bob", "steve", "jane"};
string result = strings.Aggregate((working, next) => working + next);
Console.WriteLine(result);
Note: Aggregate is in the System.Linq namespace as an extension method.
注意:Aggregate 作为扩展方法位于 System.Linq 命名空间中。
回答by Sam Harwell
Depending on how the JIT optimizes it, either string.Concat() or Marc's method with StringBuilder could be faster. Since you're using Linq here, I'll assume performance isn't the absolute #1 requirement, in which case I'd go with the easiest to read:
根据 JIT 如何优化它,string.Concat() 或 Marc 的 StringBuilder 方法可能会更快。由于您在这里使用 Linq,我假设性能不是绝对的 #1 要求,在这种情况下,我会选择最容易阅读的方式:
string.Concat(data.ToArray());
Edit: if and only if data is IEnumerable of a value type, you'll need to cast it to an IEnumerable<object>:
编辑:当且仅当数据是值类型的IEnumerable 时,您需要将其转换为 IEnumerable<object>:
string.Concat(data.Cast<object>().ToArray())
Edit 2: I don't really mean Linq is slow. I only mean that the speed difference between the two ways I mentioned should be extremely minimal if even measurable.
编辑 2:我并不是说 Linq 很慢。我的意思是,我提到的两种方式之间的速度差异即使可以测量也应该非常小。
Edit 3: The JIT optimizes almost all operations on the String class, so the single call to the internal runtime of string.Concat really could be faster than using StringBuilder. I'm not sure it is, but you should test it to make sure.
编辑 3:JIT 优化了 String 类上的几乎所有操作,因此对 string.Concat 的内部运行时的单个调用确实比使用 StringBuilder 更快。我不确定它是不是,但你应该测试它以确保。
回答by Pavel Minaev
data.ToList().Aggregate(new StringBuilder(), (sb, s) => sb.Append(s)).ToString();
回答by dmon
Alternative:
选择:
>>> data = ['Some ', 'resulting ', 'data here.']
>>> s = ''.join(data)
>>> s
'Some resulting data here.'
回答by Murtaza Malik
You can use this statement. String.Join("",someOtherList);
您可以使用此语句。String.Join("",someOtherList);