Possible Duplica开发者_如何学JAVAte:
Split string, convert ToList<int>() in one line…
i have a string that looks like this.
string s = "1,6,4,3,5,7,4";
and i want to convert this into an array of integers.
what is the best and fastest way of doing this in C#?
use split method.
int[] array = s.Split(',').Select(str => int.Parse(str)).ToArray();
Hmm, don't know if it is fastest way, however it is the simplest way :)
Hope this helps :)
int[] i = Array.ConvertAll(s.Split(','), new Converter<string, int>(delegate (string str) { return int.Parse(str); } ));
精彩评论