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.

傳回

Boolean

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的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.TryParseDouble.TryParse 整數或浮點數類型的或。To 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 標準中的某些數位是由構成代理組的兩個物件所表示 CharHowever, 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) false 如果傳遞的是高代理或此字元的低代理,則方法會傳回。As 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.

傳回

Boolean

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的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.TryParseDouble.TryParse 整數或浮點數類型的或。To 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.

如果 Char 位於位置的物件 index 是有效代理組的第一個字元,則此 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. 如範例的輸出所示,如果將 IsNumber(String, Int32) true 希臘愛琴大學數位1的高代理傳遞給它,方法會傳回。As 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

另請參閱

適用於