次の方法で共有


CharUnicodeInfo.GetUnicodeCategory Method (String, Int32)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Updated: December 2010

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

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

Syntax

'Declaration
Public Shared Function GetUnicodeCategory ( _
    s As String, _
    index As Integer _
) As UnicodeCategory
public static UnicodeCategory GetUnicodeCategory(
    string s,
    int index
)

Parameters

  • s
    Type: System.String
    The String containing the Unicode character for which to get the Unicode category.
  • index
    Type: System.Int32
    The index of the Unicode character for which to get the Unicode category.

Return Value

Type: System.Globalization.UnicodeCategory
A UnicodeCategory value indicating the category of the character at the specified index of the specified string.

Exceptions

Exception Condition
ArgumentNullException

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

ArgumentOutOfRangeException

index is outside the range of valid indexes in s.

Remarks

The Unicode characters are divided into categories. 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.

If the Char object at position index is the first character of a valid surrogate pair, the GetUnicodeCategory method returns the Unicode category of the surrogate pair instead of returning UnicodeCategory.Surrogate. For example, the Ugaritic alphabet occupies code points U+10380 to U+1039F. The following example instantiates a string that represents UGARITIC LETTER ALPA (U+10380), which is the first letter of the Ugaritic alphabet. As the output from the example shows, the GetUnicodeCategory method returns UnicodeCategory.OtherLetter if it is passed the high surrogate of this character, which indicates that it considers the surrogate pair. However, if it is passed the low surrogate, it considers only the low surrogate in isolation and returns UnicodeCategory.Surrogate.

Note that CharUnicodeInfo.GetUnicodeCategory(String, Int32) method does not always return the same UnicodeCategory value as the Char.GetUnicodeCategory(String, Int32) method when passed a particular character as a parameter. The CharUnicodeInfo.GetUnicodeCategory method is designed to reflect the current version of the Unicode standard. In contrast, although the Char.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.

Examples

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

Imports System.Globalization

Public Class Example
   Public Shared Sub Demo(ByVal outputBlock As System.Windows.Controls.TextBlock)
      ' The string to get information for.
      Dim s As String = "a9" & ChrW(&h0393) & ChrW(&h00B2) & Chrw(&h00BC) & _
                        ChrW(&h0BEF) & ChrW(&h0BF0) & ChrW(&h2788)
      outputBlock.Text += String.Format("String: {0}", s) & vbCrLf

      ' Print the values for each of the characters in the string.
      outputBlock.Text &= "index c  Num   UnicodeCategory" & vbCrLf
      For i As Integer = 0 To s.Length - 1
         outputBlock.Text += String.Format("{0,-5} {1,-3}", i, s(i))
         outputBlock.Text += String.Format(" {0,-5}", CharUnicodeInfo.GetNumericValue(s, i))
         outputBlock.Text += String.Format("{0}", CharUnicodeInfo.GetUnicodeCategory(s, i)) & vbCrLf
      Next 
   End Sub  
End Class 
' This example produces the following output. 
'       String: a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788
'       index c  Num   UnicodeCategory
'       0     a   -1   LowercaseLetter
'       1     9   9    DecimalDigitNumber
'       2     \u0393   -1    UppercaseLetter
'       3     \u00B2   2     OtherNumber
'       4     \u00BC   0.25  OtherNumber
'       5     \u0BEF   9     DecimalDigitNumber
'       6     \u0BF0   10    OtherNumber
'       7     \u2788   9     OtherNumber
using System;
using System.Globalization;

public class Example
{
   public static void Demo(System.Windows.Controls.TextBlock outputBlock)
   {
      // The string to get information for.
      String s = "a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788";
      outputBlock.Text += String.Format("String: {0}", s) + "\n";

      // Print the values for each of the characters in the string.
      outputBlock.Text += "index c  Num   Dig   Dec   UnicodeCategory" + "\n";
      for (int i = 0; i < s.Length; i++)
      {
         outputBlock.Text += String.Format("{0,-5} {1,-3}", i, s[i]);
         outputBlock.Text += String.Format(" {0,-5}", CharUnicodeInfo.GetNumericValue(s, i));
         outputBlock.Text += String.Format("{0}", CharUnicodeInfo.GetUnicodeCategory(s, i)) + "\n";
      }
   }
}
/*
This code produces the following output.  
   String: a9\u0393\u00B2\u00BC\u0BEF\u0BF0\u2788
   index c  Num   UnicodeCategory
   0     a   -1   LowercaseLetter
   1     9   9    DecimalDigitNumber
   2     \u0393   -1    UppercaseLetter
   3     \u00B2   2     OtherNumber
   4     \u00BC   0.25  OtherNumber
   5     \u0BEF   9     DecimalDigitNumber
   6     \u0BF0   10    OtherNumber
   7     \u2788   9     OtherNumber
*/

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Change History

Date

History

Reason

December 2010

Added information about how the method handles surrogate pairs.

Information enhancement.