C# 提取字符串中的数字 - Ms_GxName2 示例
要提取 Ms_GxName2 的数字,可以使用正则表达式来匹配数字部分。以下是一个示例代码:
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string input = 'Ms_GxName2';
string pattern = @"\d+";
Match match = Regex.Match(input, pattern);
if (match.Success)
{
string number = match.Value;
Console.WriteLine(number);
}
else
{
Console.WriteLine('未找到匹配的数字');
}
}
}
在上述代码中,使用了正则表达式模式 \d+ 来匹配一个或多个数字。然后使用 Regex.Match 方法在输入字符串中找到第一个匹配项。如果找到匹配项,将提取的数字打印出来;如果没有找到匹配项,则打印出一条相应的提示信息。
原文地址: https://www.cveoy.top/t/topic/qC4v 著作权归作者所有。请勿转载和采集!