查找并从字符串中提取一个数字
问题描述:
我有一个要求,寻找并提取包含在字符串中的数字。
I have a requirement to find and extract a number contained within a string.
例如,这些字符串:
string test = "1 test"
string test1 = " 1 test"
string test2 = "test 99"
我怎样才能做到这一点?
How can I do this?
答
\ D +
的正则表达式的整数倍。因此,
\d+
is the regex for an integer number. So
//System.Text.RegularExpressions.Regex
resultString = Regex.Match(subjectString, @"\d+").Value;
返回 subjectString
包含数字的第一次出现的字符串。
returns a string containing the first occurrence of a number in subjectString
.
Int32.Parse(resultString)
然后给你一些。