【发布时间】:2010-10-19 08:57:26
【问题描述】:
大家好 尝试将列表集合字符串转换为单行字符串时出现问题。 但是对于每个项目,我必须使用特定的格式进行编辑。
例子
List<string> items = new List<string>();
string result = string.Empty;
items.Add("First");
items.Add("Second");
items.Add("Last");
result = string.Join(",", items.ToArray());
Console.WriteLine(result); // Display: First,Second,Last
但我希望转换成这样的:
[First],[Second],[Last]
或类似的东西
--First-,--Second-,--Last-
我知道使用 foreach for 循环编写此代码的技术很少。
但它是否可以编写代码只是将列表集合中的所有项目更改为特定的模式字符串。
所以项目集合字符串包含从“First”到“\First/”,或从“Last”到“''Last'”。
注意
【问题讨论】:
-
能否请您重新表述问题。看了几遍,还是不知道哪里出了问题。
-
您可以使用初始化列表构造字符串集合(尽管它可能与您的场景不匹配):
var items = new List<string>() { "One", "Two", "Three", };。您也不必再转换为数组来使用string.Join。它现在具有此签名的覆盖:public static string Join(string separator, IEnumerable<string> values);
标签: c# string collections coding-style