开发者

converting a string to a list of integers

开发者 https://www.devze.com 2023-02-13 04:57 出处:网络
I have a Visual Studio 2008 C#.NET 3.5 application where I have a string with a list of numbers separated by a semicolon.

I have a Visual Studio 2008 C#.NET 3.5 application where I have a string with a list of numbers separated by a semicolon.

string num_list = "1;2;3;4;201;2099;84"

I would like to convert that to a List<int>. Is there an easier way than this?

List<int> foo = new List<int>开发者_StackOverflow社区;();
foreach (string num in num_list.Split(';'))
    foo.Add(Convert.ToInt32(num));

Thanks, PaulH


 List<int> foo = num_list.Split(';').Select(num => Convert.ToInt32(num)).ToList();


num_list.Split(';').Select( o => int.Parse(o)).ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消