So I have comma-separated string like 1,5,7
, so what's the most simple and native way to convert this string
to int[]
? I can 开发者_如何学Gowrite my own split function, but there's some interest how to do it in most native way.
Thanks in advance guys!
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));
精彩评论