我需要一种方法来将字符串形式的数字列表带到List对象。
以下是一个例子:
string ids = "10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19";
List<String> idList = new List<String>();
idList.SomeCoolMethodToParseTheText(ids); <------+
|
foreach (string id in idList) |
{ |
// Do stuff with each id. |
} |
|
// This is the Method that I need ----------------+
.net库中有什么东西让我自己不必写SomeCoolMethodToParseTheText
吗?
答案 0 :(得分:12)
using System.Linq;
List<string> idList = ids.Split(new[] { "\r\n" }, StringSplitOptions.None)
.ToList();
答案 1 :(得分:0)
我尝试使用ids.split(new[] { "\r\n" }, StringSplitOptions.None)
从而
foreach(string id in ids.Split(new[] { "\r\n" }, StringSplitOptions.None))
{
}
答案 2 :(得分:0)
string ids = "10\r\n11\r\n12\r\n13\r\n14\r\n15\r\n16\r\n17\r\n18\r\n19";
List<String> idList = Regex.Split(ids, "\r\n").ToList();
foreach (String id in idList)
{
//Do you stuff here
}