加入用分隔符字符串 [英] Join a string using delimiters
问题描述
什么是加入的字符串成一个组合分隔字符串列表的最佳方式。我主要关心的何时停止添加分隔符。我将使用C#我的例子,但我想这是语言无关的。
What is the best way to join a list of strings into a combined delimited string. I'm mainly concerned about when to stop adding the delimiter. I'll use C# for my examples but I would like this to be language agnostic.
编辑:我还没有使用StringBuilder的做code稍微简单
I have not used StringBuilder to make the code slightly simpler.
使用for循环
for(int i=0; i < list.Length; i++)
{
result += list[i];
if(i != list.Length - 1)
result += delimiter;
}
使用for循环设置的第一个项目previously
result = list[0];
for(int i = 1; i < list.Length; i++)
result += delimiter + list[i];
这些不会为一个IEnumerable工作,你不知道列表的长度事先让
These won't work for an IEnumerable where you don't know the length of the list beforehand so
使用foreach循环
bool first = true;
foreach(string item in list)
{
if(!first)
result += delimiter;
result += item;
first = false;
}
在foreach循环变化
这是乔恩的解决方案
StringBuilder builder = new StringBuilder();
string delimiter = "";
foreach (string item in list)
{
builder.Append(delimiter);
builder.Append(item);
delimiter = ",";
}
return builder.ToString();
使用迭代器
再次
using (IEnumerator<string> iterator = list.GetEnumerator())
{
if (!iterator.MoveNext())
return "";
StringBuilder builder = new StringBuilder(iterator.Current);
while (iterator.MoveNext())
{
builder.Append(delimiter);
builder.Append(iterator.Current);
}
return builder.ToString();
}
还有什么其他的算法有哪些?
What other algorithms are there?
推荐答案
这是不可能在这里得到真正的语言无关的答案,不同的语言和平台处理字符串方式不同,提供不同级别的内置支持加盟名单字符串。你可以采取pretty的多的一样的code两种不同的语言,这将是伟大的一个,可怕的另一回事。
It's impossible to give a truly language-agnostic answer here as different languages and platforms handle strings differently, and provide different levels of built-in support for joining lists of strings. You could take pretty much identical code in two different languages, and it would be great in one and awful in another.
在C#中,你可以使用:
In C#, you could use:
StringBuilder builder = new StringBuilder();
string delimiter = "";
foreach (string item in list)
{
builder.Append(delimiter);
builder.Append(item);
delimiter = ",";
}
return builder.ToString();
这会的 prePEND 的所有逗号,但第一个项目。类似code将是很好的在Java中了。
This will prepend a comma on all but the first item. Similar code would be good in Java too.
编辑:下面是一个另类,有点像伊恩后来的回答,但工作在一般的IEnumerable&LT;字符串&GT;
Here's an alternative, a bit like Ian's later answer but working on a general IEnumerable<string>
.
// Change to IEnumerator for the non-generic IEnumerable
using (IEnumerator<string> iterator = list.GetEnumerator())
{
if (!iterator.MoveNext())
{
return "";
}
StringBuilder builder = new StringBuilder(iterator.Current);
while (iterator.MoveNext())
{
builder.Append(delimiter);
builder.Append(iterator.Current);
}
return builder.ToString();
}
在原来的答案编辑近5年后...
EDIT nearly 5 years after the original answer...
在.NET 4中,<一个href="http://msdn.microsoft.com/en-us/library/system.string.join%28v=vs.100%29.aspx"><$c$c>string.Join$c$c>是显著超载pretty的。有过载服用的IEnumerable&LT; T&GT;
它会自动调用的ToString
,并有一个过载 IEnumerable的&LT;字符串&GT;
。所以,你不需要code以上任何更多......对于.NET,反正。
In .NET 4, string.Join
was overloaded pretty significantly. There's an overload taking IEnumerable<T>
which automatically calls ToString
, and there's an overload for IEnumerable<string>
. So you don't need the code above any more... for .NET, anyway.
这篇关于加入用分隔符字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!