开发者

what is the best way in C# to convert string into int[] [duplicate]

开发者 https://www.devze.com 2023-02-12 18:40 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplica开发者_如何学JAVAte:
This question already has answers here: Closed 11 years ago.

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

精彩评论

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