如何在 C# 中的一个字符串中查找一个数字?
使用正则表达式在字符串中查找数字。
我们设置了 Regex 模式从字符串中获取数字。
Regex r = new Regex(@"\d+");
现在,在 C# 中使用 Match 类设置字符串。
Match m = r.Match("Welcome! We are open 365 days in a year!");现在使用 Success 属性显示结果,如果在字符串中找到数字,如下面的完整代码所示 −
示例
using System;
using System.Text.RegularExpressions;
class Demo {
static void Main() {
Regex r = new Regex(@"\d+");
Match m = r.Match("Welcome! We are open 365 days in a year!");
if (m.Success) {
Console.Write("Number: ");
Console.WriteLine(m.Value);
}
}
}输出
Number: 365
广告
数据结构
网络技术
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP