PaulH
PaulH

Reputation: 7863

converting a string to a list of integers

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>();
foreach (string num in num_list.Split(';'))
    foo.Add(Convert.ToInt32(num));

Thanks, PaulH

Upvotes: 1

Views: 195

Answers (2)

tenor
tenor

Reputation: 1105

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

Upvotes: 1

Bala R
Bala R

Reputation: 108957

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

Upvotes: 5

Related Questions