语言

Char.IsNumber 方法

定义

指示 Unicode 字符是否分类为数字。

重载

名称 说明
IsNumber(Char)

指示指定的 Unicode 字符是否分类为数字。

IsNumber(String, Int32)

指示指定字符串中指定位置处的字符是否归类为数字。

IsNumber(Char)

Source:
Char.cs
Source:
Char.cs
Source:
Char.cs
Source:
Char.cs
Source:
Char.cs

指示指定的 Unicode 字符是否分类为数字。

public:
 static bool IsNumber(char c);
public static bool IsNumber(char c);
static member IsNumber : char -> bool
Public Shared Function IsNumber (c As Char) As Boolean

参数

c
Char

要计算的 Unicode 字符。

返回

true 如果 c 为数字,则为 ;否则为 false。

示例

下面的示例演示了 IsNumber.

using System;

public class IsNumberSample {
    public static void Main() {
        string str = "non-numeric";

        Console.WriteLine(Char.IsNumber('8'));		// Output: "True"
        Console.WriteLine(Char.IsNumber(str, 3));	// Output: "False"
    }
}
open System

let str = "non-numeric"

printfn $"{Char.IsNumber '8'}"      // Output: "True"
printfn $"{Char.IsNumber(str, 3)}"  // Output: "False"
Module IsNumberSample
    Sub Main()
        Dim str As String
        str = "non-numeric"

        Console.WriteLine(Char.IsNumber("8"c))      ' Output: "True"
        Console.WriteLine(Char.IsNumber(str, 3))    ' Output: "False"
    End Sub
End Module

注解

此方法确定一个 Char 是否属于任何数值 Unicode 类别。 除了包括数字之外,数字还包括字符、分数、下标、上标、罗马数字、货币分子和包围数字。 此方法与 IsDigit 该方法形成鲜明对比,该方法确定 a Char 是否为弧度 10 位。

Important

该方法 IsNumber(Char) 不旨在确定字符串是否包含数字字符(例如,通过调用字符串中每个字符的方法)。 若要确定字符串是否由数字字符组成,请调用方法的 TryParse 重载之一(例如 Int32.TryParse 整型或 Double.TryParse 浮点类型)。

有效数字是或UnicodeCategory.DecimalDigitNumberUnicodeCategory.LetterNumberUnicodeCategory.OtherNumber类别的成员。

该方法 IsNumber(Char) 假定对应于 c 单个语言字符,并检查该字符是否表示数字。 但是,Unicode 标准中的某些数字由构成代理项对的两 Char 个对象表示。 例如,爱琴海编号系统由代码点 U+10107 到 U+10133 组成。 以下示例使用该方法 ConvertFromUtf32 实例化表示爱琴海第一号的字符串。 如示例中的输出所示,如果传递了高代理项或此字符的低代理项,该方法 IsNumber(Char) 将 false 返回该方法。

int utf32 = 0x10107;      // AEGEAN NUMBER ONE
string surrogate = Char.ConvertFromUtf32(utf32);
foreach (var ch in surrogate)
   Console.WriteLine("U+{0:X4}: {1}", Convert.ToUInt16(ch),
                                    Char.IsNumber(ch));

// The example displays the following output:
//       U+D800: False
//       U+DD07: False
let utf32 = 0x10107      // AEGEAN NUMBER ONE
let surrogate = Char.ConvertFromUtf32 utf32
for ch in surrogate do
    printfn $"U+{Convert.ToUInt16 ch:X4}: {Char.IsNumber ch}"

// The example displays the following output:
//       U+D800: False
//       U+DD07: False
Dim utf32 As Integer = &h10107      ' AEGEAN NUMBER ONE
Dim surrogate As String = Char.ConvertFromUtf32(utf32)
For Each ch In surrogate
   Console.WriteLine("U+{0:X4}: {1}", Convert.ToUInt16(ch), 
                                    Char.IsNumber(ch))
Next
' The example displays the following output:
'       U+D800: False
'       U+DD07: False

另请参阅

适用于

IsNumber(String, Int32)

Source:
Char.cs
Source:
Char.cs
Source:
Char.cs
Source:
Char.cs
Source:
Char.cs

指示指定字符串中指定位置处的字符是否归类为数字。

public:
 static bool IsNumber(System::String ^ s, int index);
public static bool IsNumber(string s, int index);
static member IsNumber : string * int -> bool
Public Shared Function IsNumber (s As String, index As Integer) As Boolean

参数

s
String

字符串。

index
Int32

要计算的 s字符的位置。

返回

true如果位置indexs处的字符是数字,则为 ;否则为 false。

例外

s 是 null。

index 小于零或大于最后一 s个位置。

示例

下面的示例演示了 IsNumber.

using System;

public class IsNumberSample {
    public static void Main() {
        string str = "non-numeric";

        Console.WriteLine(Char.IsNumber('8'));		// Output: "True"
        Console.WriteLine(Char.IsNumber(str, 3));	// Output: "False"
    }
}
open System

let str = "non-numeric"

printfn $"{Char.IsNumber '8'}"      // Output: "True"
printfn $"{Char.IsNumber(str, 3)}"  // Output: "False"
Module IsNumberSample
    Sub Main()
        Dim str As String
        str = "non-numeric"

        Console.WriteLine(Char.IsNumber("8"c))      ' Output: "True"
        Console.WriteLine(Char.IsNumber(str, 3))    ' Output: "False"
    End Sub
End Module

注解

此方法确定一个 Char 是否属于任何数值 Unicode 类别。 除了包括数字之外,数字还包括字符、分数、下标、上标、罗马数字、货币分子和包围数字。 此方法与 IsDigit 该方法形成鲜明对比,该方法确定 a Char 是否为弧度 10 位。

字符串中的字符位置从零开始编制索引。

Important

该方法 IsNumber(String, Int32) 不旨在确定字符串是否包含数字字符(例如,通过调用字符串中每个字符的方法)。 若要确定字符串是否由数字字符组成,请调用方法的 TryParse 重载之一(例如 Int32.TryParse 整型或 Double.TryParse 浮点类型)。

有效数字是或UnicodeCategory.DecimalDigitNumberUnicodeCategory.LetterNumberUnicodeCategory.OtherNumber类别的成员。

Char如果位于位置index的对象是有效代理项对的第一个字符,则IsNumber(String, Int32)该方法确定代理项对是否形成数字数字。 例如,爱琴海编号系统由代码点 U+10107 到 U+10133 组成。 以下示例使用该方法 ConvertFromUtf32 实例化表示爱琴海第一号的字符串。 如示例中的输出所示,如果该方法传递了爱琴海第一号的高代理项,则 IsNumber(String, Int32) 该方法将返回 true 。 但是,如果传递低代理项,则它只考虑低代理项的类别并返回 false。

int utf32 = 0x10107;      // AEGEAN NUMBER ONE
string surrogate = Char.ConvertFromUtf32(utf32);
for (int ctr = 0; ctr < surrogate.Length; ctr++)
   Console.WriteLine("U+{0:X4} at position {1}: {2}",
                     Convert.ToUInt16(surrogate[ctr]), ctr,
                     Char.IsNumber(surrogate, ctr));
// The example displays the following output:
//       U+D800 at position 0: True
//       U+DD07 at position 1: False
let utf32 = 0x10107      // AEGEAN NUMBER ONE
let surrogate = Char.ConvertFromUtf32 utf32
for i = 0 to surrogate.Length - 1 do
    printfn $"U+{Convert.ToUInt16 surrogate[i]:X4} at position {i}: {Char.IsNumber(surrogate, i)}"
                    
// The example displays the following output:
//       U+D800 at position 0: True
//       U+DD07 at position 1: False
Dim utf32 As Integer = &h10107      ' AEGEAN NUMBER ONE
Dim surrogate As String = Char.ConvertFromUtf32(utf32)
For ctr As Integer = 0 To surrogate.Length - 1
   Console.WriteLine("U+{0:X4} at position {1}: {2}", 
                     Convert.ToUInt16(surrogate(ctr)), ctr,  
                     Char.IsNumber(surrogate, ctr))
Next
' The example displays the following output:
'       U+D800 at position 0: True
'       U+DD07 at position 1: False

另请参阅

适用于