How can I get this int[]
array after the .Split()
?
string s = "1,2,3,4";
int[] a = s.Split(',').ToArray<int>();
Split doesn't give you magically int values, it returns an array of string. So you'll have to convert.
s.Split(',').Select(x => Convert.ToInt32(x)).ToArray();
I would do as Raphaël says, but if you are unfamiliar with lambda expressions (the x => .. part) you can use this instead. Both will give you an array of int's, Raphaëls example is to be preferable, but Lambda expressions can be scary when you don't know how they work :P (Basically it means "for each string x, do Convert.ToInt32(x)".
int[] a = s.Split(',').Select(int.Parse).ToArray();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With