查找并从字符串中提取一个数字 [英] Find and extract a number from a string

查看:123
本文介绍了查找并从字符串中提取一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,寻找并提取包含在字符串中的数字。

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)然后给你一些。

这篇关于查找并从字符串中提取一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆