美文网首页
.Net Core List 转 字符串

.Net Core List 转 字符串

作者: Rinaloving | 来源:发表于2023-02-12 11:54 被阅读0次

    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(','));
    

    相关文章

      网友评论

          本文标题:.Net Core List 转 字符串

          本文链接:https://www.haomeiwen.com/subject/nnkdkdtx.html