[英]System.Array does not contain a definition for ToArray
How can I get this int[]
array after the .Split()
? 如何在
.Split()
之后得到这个int[]
数组?
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. Split不会给你神奇的int值,它会返回一个字符串数组。 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. 我会按照Raphaël的说法做,但如果你不熟悉lambda表达式(x => ..部分),你可以使用它。 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数组,Raphaëls例子更可取,但Lambda表达式在你不知道它们如何工作时可能会很吓人:P(基本上它意味着“对于每个字符串x,做Convert.ToInt32(x) ”。
int[] a = s.Split(',').Select(int.Parse).ToArray();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.