字符串列表,将一个字符串 [英] List of strings to one string

查看:151
本文介绍了字符串列表,将一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设你有一个:

List<string> los = new List<string>();

在这个疯狂的功能我们生活的世界,这些天,这其中之一将是最适​​合创建一个字符串中通过将这些:

In this crazy functional world we live in these days which one of these would be best for creating one string by concatenating these:

String.Join(String.Empty, los.ToArray());

StringBuilder builder = new StringBuilder();
los.ForEach(s => builder.Append(s));

string disp = los.Aggregate<string>((a, b) => a + b);

或纯老的StringBuilder的foreach

or Plain old StringBuilder foreach

或?有没有更好的办法

推荐答案

我会去与选项A:

String.Join(String.Empty, los.ToArray());



我的理由是,因为加入方法用于这一目的编写的。事实上,如果你看一下反射,你会看到不安全的代码被用于真正的优化。另外两个也行,但我认为加入函数为此写的,我猜,最有效的。我可能是错的...

My reasoning is because the Join method was written for that purpose. In fact if you look at Reflector, you'll see that unsafe code was used to really optimize it. The other two also WORK, but I think the Join function was written for this purpose, and I would guess, the most efficient. I could be wrong though...

根据@Nuri YILMAZ没有 .ToArray(),但这是.NET 4 +:

As per @Nuri YILMAZ without .ToArray(), but this is .NET 4+:

String.Join(String.Empty, los);

这篇关于字符串列表,将一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆