将列表转换成逗号分隔的字符串 [英] Convert List into Comma-Separated String

查看:88
本文介绍了将列表转换成逗号分隔的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下:

public void ReadListItem()
{
     List<uint> lst = new List<uint>() { 1, 2, 3, 4, 5 };
     string str = string.Empty;
     foreach (var item in lst)
         str = str + item + ",";

     str = str.Remove(str.Length - 1);
     Console.WriteLine(str);
}

输出: 1,2,3,4, 5

List< uint> 转换为最简单的方法是什么

What is the most simple way to convert the List<uint> into a comma-separated string?

推荐答案

享受!

Console.WriteLine(String.Join(",", new List<uint> { 1, 2, 3, 4, 5 }));

第一个参数:

第二个参数: new List< uint> {1,2,3,4,5})

String.Join 将列表作为第二个参数,并使用作为第一个参数传递的字符串将所有元素连接成一个字符串。

String.Join will take a list as a the second parameter and join all of the elements using the string passed as the first parameter into one single string.

这篇关于将列表转换成逗号分隔的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆