首页 > 解决方案 > 从字符串 C# 中查找并提取一个数字

问题描述

我需要查找并提取字符串中包含的数字。

例如,从这些字符串:

“O:2275000 BF:3060000 D:3260000 E:3472000 I:3918000 T:4247000 UF:4777000 A:4904000 AD:5010000 X:5243000 G:21280000”

提炼 :

1.2275000 2.3060000 3.3260000 ....

标签: c#asp.netasp.net-mvc

解决方案


这将是 :

string temp = yourText;
List<int> numbers = new List<int>();
Regex re = new Regex(@"\d+");
Match m = re.Match(temp);
while (m.Success)
{
    numbers.Add(Convert.ToInt32(m.Value));
    temp = temp.Substring(m.Index + m.Length);
    m = re.Match(temp);
}

推荐阅读