Char.IsNumber 方法

定义

指示 Unicode 字符是否属于数字类别。Indicates whether a Unicode character is categorized as a number.

重载

IsNumber(Char)

指示指定的 Unicode 字符是否属于数字类别。Indicates whether the specified Unicode character is categorized as a number.

IsNumber(String, Int32)

指示指定字符串中位于指定位置的字符是否属于数字类别。Indicates whether the character at the specified position in a specified string is categorized as a number.

IsNumber(Char)

指示指定的 Unicode 字符是否属于数字类别。Indicates whether the specified Unicode character is categorized as a number.

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 字符。The Unicode character to evaluate.

返回

如果 true 是一个数字,则为 c;否则为 falsetrue if c is a number; otherwise, false.

示例

下面的示例演示 IsNumberThe following example demonstrates IsNumber.

using namespace System;
int main()
{
   String^ str =  "non-numeric";
   Console::WriteLine( Char::IsNumber( '8' ) ); // Output: "True"
   Console::WriteLine( Char::IsNumber( str, 3 ) ); // Output: "False"
}

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"
    }
}
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 类别。This method determines whether a Char is of any numeric Unicode category. 除了包含数字以外,数字还包括字符、分数、下标、上标、罗马数字、货币分子和 encircled 数。In addition to including digits, numbers include characters, fractions, subscripts, superscripts, Roman numerals, currency numerators, and encircled numbers. 此方法与 IsDigit 方法进行了对比,后者确定 Char 是否为10基数数字。This method contrasts with the IsDigit method, which determines whether a Char is a radix-10 digit.

重要

IsNumber(Char) 方法不用于确定字符串是否包含数字字符(例如,通过对字符串中的每个字符调用方法)。The IsNumber(Char) method is not intended to determine whether a string consists of numeric characters (for example, by calling the method for each character in a string). 若要确定字符串是否包含数字字符,请调用 TryParse 方法的重载之一(如 Int32.TryParse 或整数或浮点类型的 Double.TryParseTo determine whether a string consists of numeric characters, call one of the overloads of the TryParse method (such as Int32.TryParse or Double.TryParse of an integral or floating point type.

有效数字是 UnicodeCategory.DecimalDigitNumberUnicodeCategory.LetterNumberUnicodeCategory.OtherNumber 类别的成员。Valid numbers are members of the UnicodeCategory.DecimalDigitNumber, UnicodeCategory.LetterNumber, or UnicodeCategory.OtherNumber category.

IsNumber(Char) 方法假定 c 对应于单个语言字符,并检查该字符是否表示数字。The IsNumber(Char) method assumes that c corresponds to a single linguistic character and checks whether that character represents a number. 但 Unicode 标准中的一些数字由两个构成代理项对的 Char 对象表示。However, some numbers in the Unicode standard are represented by two Char objects that form a surrogate pair. 例如,爱琴大学编号系统由代码点 U + 10107 到 U + 10133 组成。For example, the Aegean numbering system consists of code points U+10107 through U+10133. 下面的示例使用 ConvertFromUtf32 方法来实例化表示爱琴大学1的字符串。The following example uses the ConvertFromUtf32 method to instantiate a string that represents AEGEAN NUMBER ONE. 如示例的输出所示,如果传递给此字符的高代理项或低代理项,则 IsNumber(Char) 方法返回 falseAs the output from the example shows, the IsNumber(Char) method returns false if it is passed either a high surrogate or a low surrogate of this character.

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       
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)

指示指定字符串中位于指定位置的字符是否属于数字类别。Indicates whether the character at the specified position in a specified string is categorized as a number.

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

一个字符串。A string.

index
Int32

s 中要计算的字符的位置。The position of the character to evaluate in s.

返回

如果 true 中位于 index 的字符是一个数字,则为 s;否则为 falsetrue if the character at position index in s is a number; otherwise, false.

异常

snulls is null.

index 小于零或大于 s 中最后一个位置。index is less than zero or greater than the last position in s.

示例

下面的示例演示 IsNumberThe following example demonstrates IsNumber.

using namespace System;
int main()
{
   String^ str =  "non-numeric";
   Console::WriteLine( Char::IsNumber( '8' ) ); // Output: "True"
   Console::WriteLine( Char::IsNumber( str, 3 ) ); // Output: "False"
}

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"
    }
}
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 类别。This method determines whether a Char is of any numeric Unicode category. 除了包含数字以外,数字还包括字符、分数、下标、上标、罗马数字、货币分子和 encircled 数。In addition to including digits, numbers include characters, fractions, subscripts, superscripts, Roman numerals, currency numerators, and encircled numbers. 此方法与 IsDigit 方法进行了对比,后者确定 Char 是否为10基数数字。This method contrasts with the IsDigit method, which determines whether a Char is a radix-10 digit.

字符串中的字符位置从零开始索引。Character positions in a string are indexed starting from zero.

重要

IsNumber(String, Int32) 方法不用于确定字符串是否包含数字字符(例如,通过对字符串中的每个字符调用方法)。The IsNumber(String, Int32) method is not intended to determine whether a string consists of numeric characters (for example, by calling the method for each character in a string). 若要确定字符串是否包含数字字符,请调用 TryParse 方法的重载之一(如 Int32.TryParse 或整数或浮点类型的 Double.TryParseTo determine whether a string consists of numeric characters, call one of the overloads of the TryParse method (such as Int32.TryParse or Double.TryParse of an integral or floating point type.

有效数字是 UnicodeCategory.DecimalDigitNumberUnicodeCategory.LetterNumberUnicodeCategory.OtherNumber 类别的成员。Valid numbers are members of the UnicodeCategory.DecimalDigitNumber, UnicodeCategory.LetterNumber, or UnicodeCategory.OtherNumber category.

如果位置 indexChar 对象是有效代理项对中的第一个字符,则 IsNumber(String, Int32) 方法将确定代理项对是否构成数字。If the Char object at position index is the first character of a valid surrogate pair, the IsNumber(String, Int32) method determines whether the surrogate pair forms a numeric digit. 例如,爱琴大学编号系统由代码点 U + 10107 到 U + 10133 组成。For example, the Aegean numbering system consists of code points U+10107 through U+10133. 下面的示例使用 ConvertFromUtf32 方法来实例化表示爱琴大学1的字符串。The following example uses the ConvertFromUtf32 method to instantiate a string that represents AEGEAN NUMBER ONE. 如示例的输出所示,如果向传递了爱琴大学 NUMBER ONE 的高代理项,则 IsNumber(String, Int32) 方法返回 trueAs the output from the example shows, the IsNumber(String, Int32) method returns true if it is passed the high surrogate of AEGEAN NUMBER ONE. 但是,如果将其传递到低代理项,则只考虑低代理项的类别,并返回 falseHowever, if it is passed the low surrogate, it considers only the category of the low surrogate and returns 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
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

另请参阅

适用于