要提取字符串中的数字,可以使用正则表达式。以下是一个示例代码,可以提取字符串中的数字:

using System;
using System.Text.RegularExpressions;

public class Program
{
    public 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);
        }
    }
}

在上面的代码中,我们使用了正则表达式 \d+ 来匹配一个或多个数字。然后,我们使用 Regex.Match 方法来在输入字符串中查找匹配项。如果找到了匹配项,我们可以使用 match.Value 属性来获取匹配到的数字。

上述代码将输出 2,因为 Ms_GxName2 中的唯一数字是 2

C# 提取字符串中的数字:示例代码

原文地址: https://www.cveoy.top/t/topic/qC4E 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录