开发者

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

This question already has answers here: C# string[] to int[] [duplicate] (7 answers) Closed 7 years ago.

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));
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜