[英]split array and get last value of each index
I have a string which looks like this 我有一个看起来像这样的字符串
string sortOrder= "download-15104,download-15103,download-15105,download-15106,download-15107,download-16104,download-16105";
And i want just ids . 我只想要身份证。 So , i did this
所以,我做到了
var ids= new List<int>();
var sortOrderArray = sortOrder.Split(',');
foreach (var item in sortOrderArray)
{
var obj = item.Split('-');
ids.Add(int.Parse(obj[1]));
}
Is there any other way to do this and do this quick ? 还有其他方法可以做到这一点并且快速做到吗?
You could use LINQ: 您可以使用LINQ:
var ids = input.Split(',').Select(x => int.Parse(x.Split('0')[1])).ToList();
But, it will not be faster . 但是, 它不会更快 。 It uses loops internally anyway.
无论如何,它在内部使用循环。 It may just be much more readable.
它可能更具可读性。
var ids=Regex.Matches(sortOrder,@"\d+(?=,|$)")
.Cast<Match>()
.Select(m=>int.Parse(m.Value));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.