Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从字符串末尾提取整数_C#_String_List - Fatal编程技术网

C# 从字符串末尾提取整数

C# 从字符串末尾提取整数,c#,string,list,C#,String,List,我在列表()中有多个ID 下面的代码从集合中将最后一个id提取为整数,并忽略以无整数值结尾的id List<int> intList = IDList.Where(a => Regex.IsMatch(a, @"\d+$") == true) .Select(x => int.Parse(Regex.Match(x, @"\d+$").Value)).ToList(); List intList=IDList.Where(a=>Regex.IsMatch(a,@“

我在
列表()中有多个ID


下面的代码从集合中将最后一个id提取为整数,并忽略以无整数值结尾的id

List<int> intList = IDList.Where(a => Regex.IsMatch(a, @"\d+$") == true)
   .Select(x => int.Parse(Regex.Match(x, @"\d+$").Value)).ToList();
List intList=IDList.Where(a=>Regex.IsMatch(a,@“\d+$”==true)
.Select(x=>int.Parse(Regex.Match(x,@“\d+$”).Value)).ToList();

下面的代码从集合中将最后一个id提取为整数,并忽略以无整数值结尾的id

List<int> intList = IDList.Where(a => Regex.IsMatch(a, @"\d+$") == true)
   .Select(x => int.Parse(Regex.Match(x, @"\d+$").Value)).ToList();
List intList=IDList.Where(a=>Regex.IsMatch(a,@“\d+$”==true)
.Select(x=>int.Parse(Regex.Match(x,@“\d+$”).Value)).ToList();

我假设您想要最后的号码:

var res = IDList.Select(x => int.Parse(Regex.Match(x, @"\d+$").Value)).ToList();

我想你想要最后的数字:

var res = IDList.Select(x => int.Parse(Regex.Match(x, @"\d+$").Value)).ToList();

如果ID始终位于末尾,则可以使用
LINQ
解决方案而不是
Regex

var query = IDList.Select(id => 
    int.Parse(new string(id.Reverse()
        .TakeWhile(x => char.IsNumber(x))
        .Reverse().ToArray())));

这个想法是从最后一个字符开始,直到它找不到数字为止。无论得到什么,都将其转换为
int
。此解决方案的优点是它真正代表了您指定的内容。

如果ID始终位于末尾,则可以使用
LINQ
解决方案而不是
Regex

var query = IDList.Select(id => 
    int.Parse(new string(id.Reverse()
        .TakeWhile(x => char.IsNumber(x))
        .Reverse().ToArray())));
这个想法是从最后一个字符开始,直到它找不到数字为止。无论得到什么,都将其转换为
int
。这个解决方案的好处在于它真正代表了您指定的内容。

根据

规则:字符串始终以长度为1到n的id结尾,并且 仅为int

模式只不过是

  [0-9]{1,n}$

  [0-9] - ints only 
  {1,n} - from 1 to n (both 1 and n are included)
  $     - string always ends with
可能的实现可能是这样的

  int n = 5; //TODO: put actual value
  String pattern = "[0-9]{1," + n.ToString() + "}$";

  List<int> intList = IDList
    .Select(line => int.Parse(Regex.Match(line, pattern).Value))
    .ToList();
那么根据

规则:字符串始终以长度为1到n的id结尾,并且 仅为int

模式只不过是

  [0-9]{1,n}$

  [0-9] - ints only 
  {1,n} - from 1 to n (both 1 and n are included)
  $     - string always ends with
可能的实现可能是这样的

  int n = 5; //TODO: put actual value
  String pattern = "[0-9]{1," + n.ToString() + "}$";

  List<int> intList = IDList
    .Select(line => int.Parse(Regex.Match(line, pattern).Value))
    .ToList();

这里有另一种LINQ方法,如果数字总是在末尾,并且不可能出现负值,那么这种方法就可以工作。跳过无效字符串:

 List<int> intList = IDList
    .Select(s => s.Reverse().TakeWhile(Char.IsDigit))
    .Where(digits => digits.Any())
    .Select(digits => int.Parse(String.Concat(digits.Reverse())))
    .ToList();
List intList=IDList
.Select(s=>s.Reverse().TakeWhile(Char.IsDigit))
.Where(digits=>digits.Any())
.Select(digits=>int.Parse(String.Concat(digits.Reverse()))
.ToList();

(编辑:类似于)

这里有另一种LINQ方法,如果数字始终在末尾,并且不可能出现负值,则该方法有效。跳过无效字符串:

 List<int> intList = IDList
    .Select(s => s.Reverse().TakeWhile(Char.IsDigit))
    .Where(digits => digits.Any())
    .Select(digits => int.Parse(String.Concat(digits.Reverse())))
    .ToList();
List intList=IDList
.Select(s=>s.Reverse().TakeWhile(Char.IsDigit))
.Where(digits=>digits.Any())
.Select(digits=>int.Parse(String.Concat(digits.Reverse()))
.ToList();

(编辑:类似于)

“我的解决方案不起作用”,它能做什么是您不期望的?你想找回什么?最后的数字?要忽略减号吗?是否应该将<代码>“1006”<代码>转换成<代码> 1006 <代码>?ID总是在末尾吗?@ DmitryBychenko是肯定的,Ian yesThen你可以考虑<代码> LINQ解决方案<代码> TakeWhile(x= > char。IsNumber(x))< /C> >而不是<代码> ReXEX 。“我的解决方案不起作用”,它做了什么你不期望的事情?你想找回什么?最后的数字?要忽略减号吗?是否应该将<代码>“1006”<代码>转换成<代码> 1006 <代码>?ID总是在末尾吗?@ DmitryBychenko是肯定的,Ian yesThen你可以考虑<代码> LINQ解决方案<代码> TakeWhile(x= > char。IsNumber(x))< /C> >而不是<代码> ReXEX 。有没有办法防止id无效时出现异常?e、 g.“abc”是否忽略结果列表中的这一项?是的,您可以使用我更新的代码忽略它们,如果结束值不是有效的int,那么它将忽略它们,尝试我的最新更新有没有办法防止id无效时出现异常?e、 g.“abc”是否忽略结果列表中的此项?是的,您可以使用我更新的代码忽略它们,如果结束值不是有效的整数,则忽略它们,尝试我的最新更新我的解决方案有什么不同?它只是上述解决方案的副本。抱歉,当我发布我的解决方案时,您已经发布了您的解决方案。在发布我的解决方案之前,我应该先做F5。我的解决方案有什么不同?它只是上述解决方案的副本。对不起,当我发布我的解决方案时,您已经发布了您的解决方案。我应该先发F5再发我的。