C# 使用 LINQ 在一行代码中将 string[] 转换为 int[]
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1297231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string[] to int[] in one line of code using LINQ
提问by abatishchev
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:
我需要一个“真实”整数数组来进一步推动它:
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?
有没有其他方法可以完成任务?
采纳答案by Ahmad Mageed
Given an array you can use the Array.ConvertAll
method:
给定一个数组,您可以使用该Array.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();
回答by Simon Fox
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));
回答by sepp2k
var list = arr.Select(i => Int32.Parse(i));
回答by Rob
var asIntegers = arr.Select(s => int.Parse(s)).ToArray();
Have to make sure you are not getting an IEnumerable<int>
as a return
必须确保你没有得到IEnumerable<int>
回报
回答by A.Dara
you can simply cast a string array to int array by:
您可以通过以下方式简单地将字符串数组转换为 int 数组:
var converted = arr.Select(int.Parse)
回答by Slai
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();
var a = Array.ConvertAll(arr, s => int.TryParse(s, out var i) ? i : 0);