如何将逗号分隔的字符串转换为List<int>?

267
string tags = "9,3,12,43,2"

List<int> TagIds = tags.Split(',');

这不起作用是因为split方法返回一个字符串数组。

11个回答

-3
很简单。首先将字符串分割。 去除逗号(,)后面的空格。 然后使用系统定义的ToList()函数。
string inputText = "text1, text2"

去除逗号后面的空格并将这个逗号分隔的文本转换为列表

List<string> resultList = (inputText.Split(',')).Select(t => t).ToList();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接