CharUnicodeInfo.GetUnicodeCategory 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
取得 Unicode 字元的 Unicode 類別。
多載
| 名稱 | Description |
|---|---|
| GetUnicodeCategory(Char) |
取得指定字元的 Unicode 類別。 |
| GetUnicodeCategory(Int32) |
取得指定字元的 Unicode 類別。 |
| GetUnicodeCategory(String, Int32) |
取得字元在指定字串索引處的 Unicode 類別。 |
GetUnicodeCategory(Char)
取得指定字元的 Unicode 類別。
public:
static System::Globalization::UnicodeCategory GetUnicodeCategory(char ch);
public static System.Globalization.UnicodeCategory GetUnicodeCategory(char ch);
static member GetUnicodeCategory : char -> System.Globalization.UnicodeCategory
Public Shared Function GetUnicodeCategory (ch As Char) As UnicodeCategory
參數
- ch
- Char
Unicode 字元,該字元需取得 Unicode 類別。
傳回
一個 UnicodeCategory 表示指定字元類別的值。
範例
以下程式碼範例展示了不同字元類型中各方法回傳的值。
using System;
using System.Globalization;
public class SamplesCharUnicodeInfo {
public static void Main() {
Console.WriteLine( " c Num Dig Dec UnicodeCategory" );
Console.Write( "U+0061 LATIN SMALL LETTER A " );
PrintProperties( 'a' );
Console.Write( "U+0393 GREEK CAPITAL LETTER GAMMA " );
PrintProperties( '\u0393' );
Console.Write( "U+0039 DIGIT NINE " );
PrintProperties( '9' );
Console.Write( "U+00B2 SUPERSCRIPT TWO " );
PrintProperties( '\u00B2' );
Console.Write( "U+00BC VULGAR FRACTION ONE QUARTER " );
PrintProperties( '\u00BC' );
Console.Write( "U+0BEF TAMIL DIGIT NINE " );
PrintProperties( '\u0BEF' );
Console.Write( "U+0BF0 TAMIL NUMBER TEN " );
PrintProperties( '\u0BF0' );
Console.Write( "U+0F33 TIBETAN DIGIT HALF ZERO " );
PrintProperties( '\u0F33' );
Console.Write( "U+2788 CIRCLED SANS-SERIF DIGIT NINE " );
PrintProperties( '\u2788' );
}
public static void PrintProperties( char c ) {
Console.Write( " {0,-3}", c );
Console.Write( " {0,-5}", CharUnicodeInfo.GetNumericValue( c ) );
Console.Write( " {0,-5}", CharUnicodeInfo.GetDigitValue( c ) );
Console.Write( " {0,-5}", CharUnicodeInfo.GetDecimalDigitValue( c ) );
Console.WriteLine( "{0}", CharUnicodeInfo.GetUnicodeCategory( c ) );
}
}
/*
This code produces the following output. Some characters might not display at the console.
c Num Dig Dec UnicodeCategory
U+0061 LATIN SMALL LETTER A a -1 -1 -1 LowercaseLetter
U+0393 GREEK CAPITAL LETTER GAMMA Γ -1 -1 -1 UppercaseLetter
U+0039 DIGIT NINE 9 9 9 9 DecimalDigitNumber
U+00B2 SUPERSCRIPT TWO ² 2 2 -1 OtherNumber
U+00BC VULGAR FRACTION ONE QUARTER ¼ 0.25 -1 -1 OtherNumber
U+0BEF TAMIL DIGIT NINE ௯ 9 9 9 DecimalDigitNumber
U+0BF0 TAMIL NUMBER TEN ௰ 10 -1 -1 OtherNumber
U+0F33 TIBETAN DIGIT HALF ZERO ༳ -0.5 -1 -1 OtherNumber
U+2788 CIRCLED SANS-SERIF DIGIT NINE ➈ 9 9 -1 OtherNumber
*/
Imports System.Globalization
Public Class SamplesCharUnicodeInfo
Public Shared Sub Main()
Console.WriteLine(" c Num Dig Dec UnicodeCategory")
Console.Write("U+0061 LATIN SMALL LETTER A ")
PrintProperties("a"c)
Console.Write("U+0393 GREEK CAPITAL LETTER GAMMA ")
PrintProperties(ChrW(&H0393))
Console.Write("U+0039 DIGIT NINE ")
PrintProperties("9"c)
Console.Write("U+00B2 SUPERSCRIPT TWO ")
PrintProperties(ChrW(&H00B2))
Console.Write("U+00BC VULGAR FRACTION ONE QUARTER ")
PrintProperties(ChrW(&H00BC))
Console.Write("U+0BEF TAMIL DIGIT NINE ")
PrintProperties(ChrW(&H0BEF))
Console.Write("U+0BF0 TAMIL NUMBER TEN ")
PrintProperties(ChrW(&H0BF0))
Console.Write("U+0F33 TIBETAN DIGIT HALF ZERO ")
PrintProperties(ChrW(&H0F33))
Console.Write("U+2788 CIRCLED SANS-SERIF DIGIT NINE ")
PrintProperties(ChrW(&H2788))
End Sub
Public Shared Sub PrintProperties(c As Char)
Console.Write(" {0,-3}", c)
Console.Write(" {0,-5}", CharUnicodeInfo.GetNumericValue(c))
Console.Write(" {0,-5}", CharUnicodeInfo.GetDigitValue(c))
Console.Write(" {0,-5}", CharUnicodeInfo.GetDecimalDigitValue(c))
Console.WriteLine("{0}", CharUnicodeInfo.GetUnicodeCategory(c))
End Sub
End Class
'This code produces the following output. Some characters might not display at the console.
'
' c Num Dig Dec UnicodeCategory
'U+0061 LATIN SMALL LETTER A a -1 -1 -1 LowercaseLetter
'U+0393 GREEK CAPITAL LETTER GAMMA Γ -1 -1 -1 UppercaseLetter
'U+0039 DIGIT NINE 9 9 9 9 DecimalDigitNumber
'U+00B2 SUPERSCRIPT TWO ² 2 2 -1 OtherNumber
'U+00BC VULGAR FRACTION ONE QUARTER ¼ 0.25 -1 -1 OtherNumber
'U+0BEF TAMIL DIGIT NINE ௯ 9 9 9 DecimalDigitNumber
'U+0BF0 TAMIL NUMBER TEN ௰ 10 -1 -1 OtherNumber
'U+0F33 TIBETAN DIGIT HALF ZERO ༳ -0.5 -1 -1 OtherNumber
'U+2788 CIRCLED SANS-SERIF DIGIT NINE ➈ 9 9 -1 OtherNumber
備註
Unicode 字元被細分為不同類別。 角色的類別是其屬性之一。 例如,一個字元可能是大寫字母、小寫字母、十進位數字、字母數字、連接標點符號、數學符號或貨幣符號。 列 UnicodeCategory 舉定義了 Unicode 字元的類別。 欲了解更多關於 Unicode 字元的資訊,請參閱 Unicode 標準。
此 GetUnicodeCategory 方法假設 對 ch 應單一語言特徵,並回傳其類別。 這表示對於替代配對,它回傳 UnicodeCategory.Surrogate 的不是替代者所屬的類別。 例如,烏加里特字母佔據代碼點 U+10380 至 U+1039F。 以下範例使用此 ConvertFromUtf32 方法實例表示烏加里特字母 ALPA(U+10380),即烏加里特字母表的第一個字母。 如範例輸出所示, IsNumber(Char) 若該方法被傳遞該字元的高代或低代替,則會返回 false 。
int utf32 = 0x10380; // UGARITIC LETTER ALPA
string surrogate = Char.ConvertFromUtf32(utf32);
foreach (var ch in surrogate)
Console.WriteLine($"U+{(ushort)ch:X4}: {System.Globalization.CharUnicodeInfo.GetUnicodeCategory(ch):G}");
// The example displays the following output:
// U+D800: Surrogate
// U+DF80: Surrogate
Dim utf32 As Integer = &h10380 ' UGARITIC LETTER ALPA
Dim surrogate As String = Char.ConvertFromUtf32(utf32)
For Each ch In surrogate
Console.WriteLine("U+{0:X4}: {1:G}",
Convert.ToUInt16(ch),
System.Globalization.CharUnicodeInfo.GetUnicodeCategory(ch))
Next
' The example displays the following output:
' U+D800: Surrogate
' U+DF80: Surrogate
注意 在CharUnicodeInfo.GetUnicodeCategory傳遞特定字元作為參數時,並不總是回傳與方法UnicodeCategory相同的Char.GetUnicodeCategory值。 此 CharUnicodeInfo.GetUnicodeCategory 方法設計以反映現行 Unicode 標準版本。 相較之下,雖然此 Char.GetUnicodeCategory 方法通常反映現行 Unicode 標準版本,但可能會回傳基於先前標準版本的字元類別,或為保留向下相容性而回傳與現行標準不同的類別。
另請參閱
適用於
GetUnicodeCategory(Int32)
取得指定字元的 Unicode 類別。
public:
static System::Globalization::UnicodeCategory GetUnicodeCategory(int codePoint);
public static System.Globalization.UnicodeCategory GetUnicodeCategory(int codePoint);
static member GetUnicodeCategory : int -> System.Globalization.UnicodeCategory
Public Shared Function GetUnicodeCategory (codePoint As Integer) As UnicodeCategory
參數
- codePoint
- Int32
一個代表 Unicode 字元的 32 位元碼點值的數字。
傳回
一個 UnicodeCategory 表示指定字元類別的值。
適用於
GetUnicodeCategory(String, Int32)
取得字元在指定字串索引處的 Unicode 類別。
public:
static System::Globalization::UnicodeCategory GetUnicodeCategory(System::String ^ s, int index);
public static System.Globalization.UnicodeCategory GetUnicodeCategory(string s, int index);
static member GetUnicodeCategory : string * int -> System.Globalization.UnicodeCategory
Public Shared Function GetUnicodeCategory (s As String, index As Integer) As UnicodeCategory
參數
- index
- Int32
Unicode 字元的索引,用來取得 Unicode 類別。
傳回
一個 UnicodeCategory 表示字元在指定字串索引下類別的值。
例外狀況
s 是 null。
index 不在有效 s索引範圍內。
範例
以下程式碼範例展示了不同字元類型中各方法回傳的值。
using System;
using System.Globalization;
public class SamplesCharUnicodeInfo {
public static void Main() {
// The String to get information for.
String s = "a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788";
Console.WriteLine( "String: {0}", s );
// Print the values for each of the characters in the string.
Console.WriteLine( "index c Num Dig Dec UnicodeCategory" );
for ( int i = 0; i < s.Length; i++ ) {
Console.Write( "{0,-5} {1,-3}", i, s[i] );
Console.Write( " {0,-5}", CharUnicodeInfo.GetNumericValue( s, i ) );
Console.Write( " {0,-5}", CharUnicodeInfo.GetDigitValue( s, i ) );
Console.Write( " {0,-5}", CharUnicodeInfo.GetDecimalDigitValue( s, i ) );
Console.WriteLine( "{0}", CharUnicodeInfo.GetUnicodeCategory( s, i ) );
}
}
}
/*
This code produces the following output. Some characters might not display at the console.
String: a9Γ²¼௯௰➈
index c Num Dig Dec UnicodeCategory
0 a -1 -1 -1 LowercaseLetter
1 9 9 9 9 DecimalDigitNumber
2 Γ -1 -1 -1 UppercaseLetter
3 ² 2 2 -1 OtherNumber
4 ¼ 0.25 -1 -1 OtherNumber
5 ௯ 9 9 9 DecimalDigitNumber
6 ௰ 10 -1 -1 OtherNumber
7 ➈ 9 9 -1 OtherNumber
*/
Imports System.Globalization
Public Class SamplesCharUnicodeInfo
Public Shared Sub Main()
' The String to get information for.
Dim s As [String] = "a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788"
Console.WriteLine("String: {0}", s)
' Print the values for each of the characters in the string.
Console.WriteLine("index c Num Dig Dec UnicodeCategory")
Dim i As Integer
For i = 0 To s.Length - 1
Console.Write("{0,-5} {1,-3}", i, s(i))
Console.Write(" {0,-5}", CharUnicodeInfo.GetNumericValue(s, i))
Console.Write(" {0,-5}", CharUnicodeInfo.GetDigitValue(s, i))
Console.Write(" {0,-5}", CharUnicodeInfo.GetDecimalDigitValue(s, i))
Console.WriteLine("{0}", CharUnicodeInfo.GetUnicodeCategory(s, i))
Next i
End Sub
End Class
'This code produces the following output. Some characters might not display at the console.
'
'String: a9Γ²¼௯௰➈
'index c Num Dig Dec UnicodeCategory
'0 a -1 -1 -1 LowercaseLetter
'1 9 9 9 9 DecimalDigitNumber
'2 Γ -1 -1 -1 UppercaseLetter
'3 ² 2 2 -1 OtherNumber
'4 ¼ 0.25 -1 -1 OtherNumber
'5 ௯ 9 9 9 DecimalDigitNumber
'6 ௰ 10 -1 -1 OtherNumber
'7 ➈ 9 9 -1 OtherNumber
備註
Unicode 字元被細分為不同類別。 角色的類別是其屬性之一。 例如,一個字元可能是大寫字母、小寫字母、十進位數字、字母數字、連接標點符號、數學符號或貨幣符號。 列 UnicodeCategory 舉定義了 Unicode 字元的類別。 欲了解更多關於 Unicode 字元的資訊,請參閱 Unicode 標準。
若 Char 位置 index 物件是有效替代對的第一個字元, GetUnicodeCategory(String, Int32) 該方法會回傳該替代對的 Unicode 類別,而非回 UnicodeCategory.Surrogate傳 。 例如,烏加里特字母佔據代碼點 U+10380 至 U+1039F。 以下範例使用此 ConvertFromUtf32 方法實例表示烏加里特字母 ALPA(U+10380),即烏加里特字母表的第一個字母。 如範例輸出所示, GetUnicodeCategory(String, Int32) 若方法通過該字元的高代詞,則返回 UnicodeCategory.OtherLetter ,表示它考慮了代節點對。 然而,若通過低代替,則僅單獨考慮低代替,返回 UnicodeCategory.Surrogate。
int utf32 = 0x10380; // UGARITIC LETTER ALPA
string surrogate = Char.ConvertFromUtf32(utf32);
for (int ctr = 0; ctr < surrogate.Length; ctr++)
Console.WriteLine($"U+{(ushort)surrogate[ctr]:X4}: {System.Globalization.CharUnicodeInfo.GetUnicodeCategory(surrogate, ctr):G}");
// The example displays the following output:
// U+D800: OtherLetter
// U+DF80: Surrogate
Dim utf32 As Integer = &h10380 ' UGARITIC LETTER ALPA
Dim surrogate As String = Char.ConvertFromUtf32(utf32)
For ctr As Integer = 0 To surrogate.Length - 1
Console.WriteLine("U+{0:X4}: {1:G}",
Convert.ToUInt16(surrogate(ctr)),
System.Globalization.CharUnicodeInfo.GetUnicodeCategory(surrogate, ctr))
Next
' The example displays the following output:
' U+D800: OtherLetter
' U+DF80: Surrogate
請注意,CharUnicodeInfo.GetUnicodeCategory當傳遞特定字元作為參數時,方法不一定會回傳與方法UnicodeCategory相同的Char.GetUnicodeCategory值。 此 CharUnicodeInfo.GetUnicodeCategory 方法設計以反映現行 Unicode 標準版本。 相較之下,雖然此 Char.GetUnicodeCategory 方法通常反映現行 Unicode 標準版本,但可能會回傳基於先前標準版本的字元類別,或為保留向下相容性而回傳與現行標準不同的類別。