List 转字符串
1. for 循环方式
List<string> strList = new List<string>(){"张三","李四","王二麻"};
string result = "";
for (int i = 0; i < strList.Count; i++) {
result = result + strList[i] + ",";
}
Console.Write("循环方式实现结果: " + result);
//去掉最后一个","使用TrimEnd。
Console.Write("循环方式实现结果: " + result.TrimEnd(','));
2. 使用String.Join
List<string> strList = new List<string>(){"张三","李四","王二麻"};
string result = String.Join(",", strList);
Console.Write("String.Join方式实现结果: " + result.TrimEnd(','));
3. 使用Linq
List<string> strList = new List<string>(){"张三","李四","王二麻"};
string result = strList .Aggregate("", (current, s) => current + (s + ","));
Console.Write("linq方式实现结果: " + result.TrimEnd(','));
网友评论