C# 拆分字符串,在一行中转换 ToList<int>()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/911717/
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
Split string, convert ToList<int>() in one line
提问by uzay95
I have a string that has numbers
我有一个带有数字的字符串
string sNumbers = "1,2,3,4,5";
I can split it then convert it to List<int>
我可以拆分它然后将其转换为 List<int>
sNumbers.Split( new[] { ',' } ).ToList<int>();
How can I convert string array to integer list?
So that I'll be able to convert string[]
to IEnumerable
如何将字符串数组转换为整数列表?这样我就可以转换string[]
为IEnumerable
采纳答案by mqp
var numbers = sNumbers.Split(',').Select(Int32.Parse).ToList();
回答by Joze
You can also do it this way without the need of Linq:
您也可以在不需要 Linq 的情况下这样做:
List<int> numbers = new List<int>( Array.ConvertAll(sNumbers.Split(','), int.Parse) );
// Uses Linq
var numbers = Array.ConvertAll(sNumbers.Split(','), int.Parse).ToList();
回答by yuxio
Joze's way also need LINQ, ToList()
is in System.Linq
namespace.
Joze 的方式也需要 LINQ,ToList()
是在System.Linq
命名空间中。
You can convert Array to List without Linq by passing the array to List constructor:
通过将数组传递给 List 构造函数,您可以在不使用 Linq 的情况下将 Array 转换为 List:
List<int> numbers = new List<int>( Array.ConvertAll(sNumbers.Split(','), int.Parse) );
回答by HyoJin KIM
On Unity3d, int.Parse
doesn't work well. So I use like bellow.
在 Unity3d 上,int.Parse
效果不佳。所以我使用如下。
List<int> intList = new List<int>( Array.ConvertAll(sNumbers.Split(','),
new Converter<string, int>((s)=>{return Convert.ToInt32(s);}) ) );
Hope this help for Unity3d Users.
希望这对 Unity3d 用户有所帮助。
回答by Pcodea Xonos
also you can use this Extension method
您也可以使用此扩展方法
public static List<int> SplitToIntList(this string list, char separator = ',')
{
return list.Split(separator).Select(Int32.Parse).ToList();
}
usage:
用法:
var numberListString = "1, 2, 3, 4";
List<int> numberList = numberListString.SplitToIntList(',');
回答by Carlos Toledo
My problem was similar but with the inconvenience that sometimes the string contains letters (sometimes empty).
我的问题很相似,但有时字符串包含字母(有时是空的),这给我带来了不便。
string sNumbers = "1,2,hh,3,4,x,5";
Trying to follow Pcode Xonos Extension Method:
尝试遵循 Pcode Xonos 扩展方法:
public static List<int> SplitToIntList(this string list, char separator = ',')
{
int result = 0;
return (from s in list.Split(',')
let isint = int.TryParse(s, out result)
let val = result
where isint
select val).ToList();
}
回答by Mukesh Kalgude
It is also possible to int array to direct assign value.
也可以通过 int 数组直接赋值。
like this
像这样
int[] numbers = sNumbers.Split(',').Select(Int32.Parse).ToArray();
回答by Charanjot
You can use this:
你可以使用这个:
List<Int32> sNumberslst = sNumbers.Split(',').ConvertIntoIntList();
回答by Adrian Filip
You can use new C# 6.0 Language Features:
您可以使用新的 C# 6.0 语言功能:
- replace delegate
(s) => { return Convert.ToInt32(s); }
with corresponding method groupConvert.ToInt32
- replace redundant constructor call:
new Converter<string, int>(Convert.ToInt32)
with:Convert.ToInt32
(s) => { return Convert.ToInt32(s); }
用相应的方法组替换委托Convert.ToInt32
- 将多余的构造函数调用替换
new Converter<string, int>(Convert.ToInt32)
为:Convert.ToInt32
The result will be:
结果将是:
var intList = new List<int>(Array.ConvertAll(sNumbers.Split(','), Convert.ToInt32));
回答by aozogul
Better use int.TryParse
to avoid exceptions;
更好地使用int.TryParse
以避免异常;
var numbers = sNumbers
.Split(',')
.Where(x => int.TryParse(x, out _))
.Select(int.Parse)
.ToList();