What's the most simple way to convert comma separated string to int[]? [duplicate] What's the most simple way to convert comma separated string to int[]? [duplicate] arrays arrays

What's the most simple way to convert comma separated string to int[]? [duplicate]


string s = "1,5,7";int[] nums = Array.ConvertAll(s.Split(','), int.Parse);

or, a LINQ-y version:

int[] nums = s.Split(',').Select(int.Parse).ToArray();

But the first one should be a teeny bit faster.


string numbers = "1,5,7";string[] pieces = numbers.Split(new string[] { "," },                                  StringSplitOptions.None);int[] array2 = new int[pieces.length];for(int i=0; i<pieces.length; i++)    array2[i] = Convert.ToInt32(pieces[i]);


Here you go.

string numbers = "1,5,7";List<int> numlist = new List<int>();foreach (string number in numbers.Split(','))    numlist.Add(Int32.Parse(number));