C#怎么把文本中的数字提出来
在C#中,可以使用正则表达式来提取文本中的数字。下面是一个示例代码:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string text = "这是一个包含数字123的文本";
// 使用正则表达式提取数字
string pattern = @"\d+";
MatchCollection matches = Regex.Matches(text, pattern);
// 输出提取到的数字
foreach (Match match in matches)
{
Console.WriteLine(match.Value);
}
}
}
上述代码使用了 Regex.Matches 方法和正则表达式 \d+ 来提取文本中的数字。 \d 表示匹配任意一个数字字符,+ 表示匹配一个或多个数字字符。
运行上述代码,将输出:
123
``
原文地址: http://www.cveoy.top/t/topic/it87 著作权归作者所有。请勿转载和采集!