判断某字符是否为数字

 可以通过以下判断字符或字符串中某个字符为数字。

  名称 说明
System_CAPS_pubmethodSystem_CAPS_static IsNumber(Char)

指示指定的 Unicode 字符是否属于数字类别。

System_CAPS_pubmethodSystem_CAPS_static IsNumber(String, Int32)

指示指定字符串中位于指定位置的字符是否属于数字类别。

IsNumber(Char)方法不应用于确定是否一个字符串是否包含数字字符 (例如,通过调用方法来对字符串中每个字符)。

 若要确定字符串是否包含数字字符,调用的重载之一TryParse方法 (如Int32.TryParseDouble.TryParse的整型或浮点型类型。 

案例

using System;
using System.Linq;

public class IsNumberSample
{
    public static void Main()
    {
        string str = "non-numeric";
        //判断8是否为数字
        Console.WriteLine(Char.IsNumber('8'));      // Output: "True"
        //判断字符串第三个字符是否为数字
        Console.WriteLine(Char.IsNumber(str, 3));   // Output: "False"
        //判断字符串最后一个字符是否为数字
        Console.WriteLine(Char.IsNumber(str.Last())); // Output: "False"
        Console.ReadKey();
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_42479664/article/details/82077806
今日推荐