C#:字符串[]为分隔字符串。是否有一个单行? [英] C#: string[] to delimited string. Is there a one-liner?
问题描述
我想preFER是一样的东西:
What I'd prefer is something like:
string[] strArray = {"Hi", "how", "are", "you"};
string strNew = strArray.Delimit(chDelimiter);
然而,不存在这样的功能。我看过了MSDN和没有看着我,将执行相同的操作功能。我看着StringBuilder的,并再次,没有站出来给我。有谁知道一个不是极其复杂单行,使数组的分隔字符串。感谢您的球员的帮助。
However, there is no such function. I've looked over MSDN and nothing looked to me as a function that would perform the same action. I looked at StringBuilder, and again, nothing stood out to me. Does anyone know of a not to extremely complicated one liner to make an array a delimited string. Thanks for your guys' help.
更新:哇,哈哈,是我不好。我一直在寻找的。加入该阵列本身上,这是窃听了我的地狱中。我甚至没有看的string.join。多谢你们。一旦它可以让我接受我应。 preciate的帮助。
UPDATE: Wow, lol, my bad. I kept looking at the .Join on the array itself and it was bugging the hell out of me. I didn't even look at String.Join. Thanks guys. Once it allows me to accept I shall. Preciate the help.
推荐答案
对于数组,你可以使用:
For arrays, you can use:
string.Join(", ", strArray);
就个人而言,我使用,我可以适用于所有类型的枚举集的扩展方法:
Personally, I use an extension method that I can apply to enumerable collections of all types:
public static string Flatten(this IEnumerable elems, string separator)
{
if (elems == null)
{
return null;
}
StringBuilder sb = new StringBuilder();
foreach (object elem in elems)
{
if (sb.Length > 0)
{
sb.Append(separator);
}
sb.Append(elem);
}
return sb.ToString();
}
...我用像这样:
...Which I use like so:
strArray.Flatten(", ");
这篇关于C#:字符串[]为分隔字符串。是否有一个单行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!