Редагувати

Поділитися через


CharUnicodeInfo Class

Definition

Retrieves information about a Unicode character. This class cannot be inherited.

public ref class CharUnicodeInfo abstract sealed
public ref class CharUnicodeInfo sealed
public static class CharUnicodeInfo
public sealed class CharUnicodeInfo
type CharUnicodeInfo = class
Public Class CharUnicodeInfo
Public NotInheritable Class CharUnicodeInfo
Inheritance
CharUnicodeInfo

Examples

The following code example shows the values returned by each method for different types of characters.

using namespace System;
using namespace System::Globalization;
void PrintProperties( Char c );
int main()
{
   Console::WriteLine( "                                        c  Num   Dig   Dec   UnicodeCategory" );
   Console::Write( "U+0061 LATIN SMALL LETTER A            " );
   PrintProperties( L'a' );
   Console::Write( "U+0393 GREEK CAPITAL LETTER GAMMA      " );
   PrintProperties( L'\u0393' );
   Console::Write( "U+0039 DIGIT NINE                      " );
   PrintProperties( L'9' );
   Console::Write( "U+00B2 SUPERSCRIPT TWO                 " );
   PrintProperties( L'\u00B2' );
   Console::Write( "U+00BC VULGAR FRACTION ONE QUARTER     " );
   PrintProperties( L'\u00BC' );
   Console::Write( "U+0BEF TAMIL DIGIT NINE                " );
   PrintProperties( L'\u0BEF' );
   Console::Write( "U+0BF0 TAMIL NUMBER TEN                " );
   PrintProperties( L'\u0BF0' );
   Console::Write( "U+0F33 TIBETAN DIGIT HALF ZERO         " );
   PrintProperties( L'\u0F33' );
   Console::Write( "U+2788 CIRCLED SANS-SERIF DIGIT NINE   " );
   PrintProperties( L'\u2788' );
}

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

*/
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

Remarks

The Unicode Standard defines a number of Unicode character categories. For example, a character might be categorized as an uppercase letter, a lowercase letter, a decimal digit number, a letter number, a paragraph separator, a math symbol, or a currency symbol. Your application can use the character category to govern string-based operations, such as parsing or extracting substring with regular expressions. The UnicodeCategory enumeration defines the possible character categories.

Use the CharUnicodeInfo class to obtain the UnicodeCategory value for a specific character. The CharUnicodeInfo class defines methods that return the following Unicode character values:

  • The specific category to which a character or surrogate pair belongs. The value returned is a member of the UnicodeCategory enumeration.

  • Numeric value. Applies only to numeric characters, including fractions, subscripts, superscripts, Roman numerals, currency numerators, encircled numbers, and script-specific digits.

  • Digit value. Applies to numeric characters that can be combined with other numeric characters to represent a whole number in a numbering system.

  • Decimal digit value. Applies only to characters that represent decimal digits in the decimal (base 10) system. A decimal digit can be one of ten digits, from zero through nine. These characters are members of the UnicodeCategory.DecimalDigitNumber category.

In addition, the CharUnicodeInfo class is used internally by a number of other .NET types and methods that rely on character classification. These include:

  • The StringInfo class, which works with textual elements instead of single characters in a string.

  • The overloads of the Char.GetUnicodeCategory method, which determine the category to which a character or surrogate pair belongs.

  • The character classes recognized by Regex, .NET's regular expression engine.

When using this class in your applications, keep in mind the following programming considerations for using the Char type. The type can be difficult to use, and strings are generally preferable for representing linguistic content.

  • A Char object does not always correspond to a single character. Although the Char type represents a single 16-bit value, some characters (such as grapheme clusters and surrogate pairs) consist of two or more UTF-16 code units. For more information, see "Char Objects and Unicode Characters" in the String class.

  • The notion of a "character" is also flexible. A character is often thought of as a glyph, but many glyphs require multiple code points. For example, ä can be represented either by two code points ("a" plus U+0308, which is the combining diaeresis), or by a single code point ("ä" or U+00A4). Some languages have many letters, characters, and glyphs that require multiple code points, which can cause confusion in linguistic content representation. For example, there is a ΰ (U+03B0, Greek small letter upsilon with dialytika and tonos), but there is no equivalent capital letter. Uppercasing such a value simply retrieves the original value.

Notes to Callers

Recognized characters and the specific categories to which they belong are defined by the Unicode standard and can change from one version of the Unicode Standard to another. Categorization of characters in a particular version of .NET Framework is based on a single version of the Unicode Standard, regardless of the underlying operating system on which .NET Framework is running. The following table lists versions of .NET Framework since .NET Framework 4 and the versions of the Unicode Standard used to classify characters.

.NET Framework version Unicode Standard version
.NET Framework 4 5.0.0
.NET Framework 4.5 5.0.0
.NET Framework 4.5.1 5.0.0
.NET Framework 4.5.2 5.0.0
.NET Framework 4.6 6.3.0
.NET Framework 4.6.1 6.3.0
.NET Framework 4.6.2 8.0.0

Each version of the Unicode standard includes information on changes to the Unicode character database since the previous version. The Unicode character database is used by the CharUnicodeInfo class for categorizing characters.

Methods

GetDecimalDigitValue(Char)

Gets the decimal digit value of the specified numeric character.

GetDecimalDigitValue(String, Int32)

Gets the decimal digit value of the numeric character at the specified index of the specified string.

GetDigitValue(Char)

Gets the digit value of the specified numeric character.

GetDigitValue(String, Int32)

Gets the digit value of the numeric character at the specified index of the specified string.

GetNumericValue(Char)

Gets the numeric value associated with the specified character.

GetNumericValue(String, Int32)

Gets the numeric value associated with the character at the specified index of the specified string.

GetUnicodeCategory(Char)

Gets the Unicode category of the specified character.

GetUnicodeCategory(Int32)

Gets the Unicode category of the specified character.

GetUnicodeCategory(String, Int32)

Gets the Unicode category of the character at the specified index of the specified string.

Applies to

See also