This documentation is archived and is not being maintained.

CharUnicodeInfo.GetUnicodeCategory Method (String, Int32)

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

Namespace: System.Globalization
Assembly: mscorlib (in mscorlib.dll)

public static UnicodeCategory GetUnicodeCategory (
	string s,
	int index
)
public static UnicodeCategory GetUnicodeCategory (
	String s, 
	int index
)
public static function GetUnicodeCategory (
	s : String, 
	index : int
) : UnicodeCategory
Not applicable.

Parameters

s

The String containing the Unicode character for which to get the Unicode category.

index

The index of the Unicode character for which to get the Unicode category.

Return Value

A UnicodeCategory value indicating the category of the character at the specified index of the specified string.

Exception typeCondition

ArgumentNullException

s is a null reference (Nothing in Visual Basic).

ArgumentOutOfRangeException

index is outside the range of valid indexes in s.

The Unicode characters are divided into categories. A character's category is one of its properties. For example, a character might be an uppercase letter, a lowercase letter, a decimal digit number, a letter number, a connector punctuation, a math symbol, or a currency symbol. The UnicodeCategory class returns the category of a Unicode character. For more information on Unicode characters, see the Unicode Standard.

Note that GetUnicodeCategory method does not always return the same UnicodeCategory value as the GetUnicodeCategory method when passed a particular character as a parameter. The GetUnicodeCategory method is designed to reflect the current version of the Unicode standard. In contrast, although the GetUnicodeCategory method usually reflects the current version of the Unicode standard, it might return a character's category based on a previous version of the standard, or it might return a category that differs from the current standard to preserve backward compatibility.

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

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\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788
index c  Num   Dig   Dec   UnicodeCategory
0     a   -1    -1    -1   LowercaseLetter
1     9   9     9     9    DecimalDigitNumber
2     \u0393   -1    -1    -1   UppercaseLetter
3     \u00B2   2     2     2    OtherNumber
4     \u00BC   0.25  -1    -1   OtherNumber
5     \u0BEF   9     9     9    DecimalDigitNumber
6     \u0BF0   10    -1    -1   OtherNumber
7     \u2788   9     9     -1   OtherNumber

*/


import System.* ;
import System.Globalization.* ;

public class SamplesCharUnicodeInfo
{
    public static void main(String[] args)
    {
        // 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.get_Length();i++) {
            Console.Write("{0,-5} {1,-3}", System.Convert.ToString(i),
                System.Convert.ToString (s.charAt(i)));
            Console.Write(" {0,-5}", System.Convert.ToString(
                CharUnicodeInfo.GetNumericValue(s, i)));
            Console.Write(" {0,-5}", System.Convert.ToString(
                CharUnicodeInfo.GetDigitValue(s, i)));
            Console.Write(" {0,-5}", System.Convert.ToString(
                CharUnicodeInfo.GetDecimalDigitValue(s, i)));
            Console.WriteLine("{0}", CharUnicodeInfo.GetUnicodeCategory(s, i));
        } 
    } //main 
} //SamplesCharUnicodeInfo

/*
This code produces the following output.  
Some characters might not display at the console.

String: a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788
index c  Num   Dig   Dec   UnicodeCategory
0     a   -1    -1    -1   LowercaseLetter
1     9   9     9     9    DecimalDigitNumber
2     \u0393   -1    -1    -1   UppercaseLetter
3     \u00B2   2     2     2    OtherNumber
4     \u00BC   0.25  -1    -1   OtherNumber
5     \u0BEF   9     9     9    DecimalDigitNumber
6     \u0BF0   10    -1    -1   OtherNumber
7     \u2788   9     9     -1   OtherNumber
*/

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

.NET Compact Framework

Supported in: 2.0

XNA Framework

Supported in: 1.0
Show: