I have an array of integers in string form:
我有一个字符串形式的整数数组:
var arr = new string[] { "1", "2", "3", "4" };
I need to an array of 'real' integers to push it further:
我需要一个'real' integers的数组来进一步推动它:
void Foo(int[] arr) { .. }
I tried to cast int and it of course failed:
我试着投射int,当然失败了:
Foo(arr.Cast<int>.ToArray());
I can do next:
我可以做下一个:
var list = new List<int>(arr.Length);
arr.ForEach(i => list.Add(Int32.Parse(i))); // maybe Convert.ToInt32() is better?
Foo(list.ToArray());
or
或
var list = new List<int>(arr.Length);
arr.ForEach(i =>
{
int j;
if (Int32.TryParse(i, out j)) // TryParse is faster, yeah
{
list.Add(j);
}
}
Foo(list.ToArray());
but both looks ugly.
但看起来丑陋。
Is there any other ways to complete the task?
完成这项任务还有其他方法吗?
6 个解决方案
#1
472
Given an array you can use the Array.ConvertAll
method:
给定一个数组,您可以使用该数组。ConvertAll方法:
int[] myInts = Array.ConvertAll(arr, s => int.Parse(s));
Thanks to Marc Gravell for pointing out that the lambda can be omitted, yielding a shorter version shown below:
感谢Marc Gravell指出lambda可以省略,从而产生一个更短的版本,如下所示:
int[] myInts = Array.ConvertAll(arr, int.Parse);
A LINQ solution is similar, except you would need the extra ToArray
call to get an array:
LINQ解决方案是类似的,除非您需要额外的ToArray调用来获得一个数组:
int[] myInts = arr.Select(int.Parse).ToArray();
#2
24
EDIT: to convert to array
编辑:转换为数组
int[] asIntegers = arr.Select(s => int.Parse(s)).ToArray();
This should do the trick:
这应该可以做到:
var asIntegers = arr.Select(s => int.Parse(s));
#3
12
To avoid exceptions with .Parse
, here are some .TryParse
alternatives.
为了避免. parse的异常,这里有一些. tryparse替代方法。
To use only the elements that can be parsed:
只使用可分析的元素:
string[] arr = { null, " ", " 1 ", " 002 ", "3.0" };
int i = 0;
var a = (from s in arr where int.TryParse(s, out i) select i).ToArray(); // a = { 1, 2 }
or
或
var a = arr.SelectMany(s => int.TryParse(s, out i) ? new[] { i } : new int[0]).ToArray();
Alternatives using 0
for the elements that can't be parsed:
对不能解析的元素使用0的替代方法:
int i;
var a = Array.ConvertAll(arr, s => int.TryParse(s, out i) ? i : 0); //a = { 0, 0, 1, 2, 0 }
or
或
var a = arr.Select((s, i) => int.TryParse(s, out i) ? i : 0).ToArray();
#4
11
you can simply cast a string array to int array by:
您可以通过以下方法将字符串数组转换为int数组:
var converted = arr.Select(int.Parse)
#5
3
var asIntegers = arr.Select(s => int.Parse(s)).ToArray();
Have to make sure you are not getting an IEnumerable<int>
as a return
必须确保您没有得到IEnumerable
#6
2
var list = arr.Select(i => Int32.Parse(i));
#1
472
Given an array you can use the Array.ConvertAll
method:
给定一个数组,您可以使用该数组。ConvertAll方法:
int[] myInts = Array.ConvertAll(arr, s => int.Parse(s));
Thanks to Marc Gravell for pointing out that the lambda can be omitted, yielding a shorter version shown below:
感谢Marc Gravell指出lambda可以省略,从而产生一个更短的版本,如下所示:
int[] myInts = Array.ConvertAll(arr, int.Parse);
A LINQ solution is similar, except you would need the extra ToArray
call to get an array:
LINQ解决方案是类似的,除非您需要额外的ToArray调用来获得一个数组:
int[] myInts = arr.Select(int.Parse).ToArray();
#2
24
EDIT: to convert to array
编辑:转换为数组
int[] asIntegers = arr.Select(s => int.Parse(s)).ToArray();
This should do the trick:
这应该可以做到:
var asIntegers = arr.Select(s => int.Parse(s));
#3
12
To avoid exceptions with .Parse
, here are some .TryParse
alternatives.
为了避免. parse的异常,这里有一些. tryparse替代方法。
To use only the elements that can be parsed:
只使用可分析的元素:
string[] arr = { null, " ", " 1 ", " 002 ", "3.0" };
int i = 0;
var a = (from s in arr where int.TryParse(s, out i) select i).ToArray(); // a = { 1, 2 }
or
或
var a = arr.SelectMany(s => int.TryParse(s, out i) ? new[] { i } : new int[0]).ToArray();
Alternatives using 0
for the elements that can't be parsed:
对不能解析的元素使用0的替代方法:
int i;
var a = Array.ConvertAll(arr, s => int.TryParse(s, out i) ? i : 0); //a = { 0, 0, 1, 2, 0 }
or
或
var a = arr.Select((s, i) => int.TryParse(s, out i) ? i : 0).ToArray();
#4
11
you can simply cast a string array to int array by:
您可以通过以下方法将字符串数组转换为int数组:
var converted = arr.Select(int.Parse)
#5
3
var asIntegers = arr.Select(s => int.Parse(s)).ToArray();
Have to make sure you are not getting an IEnumerable<int>
as a return
必须确保您没有得到IEnumerable
#6
2
var list = arr.Select(i => Int32.Parse(i));