Char.GetUnicodeCategory(String, Int32) Method with Examples in C#

The Char.GetUnicodeCategory(String, Int32) method in C# categorizes the character at the specified position in a specified string into a group identified by one of the UnicodeCategory values. This method is useful for determining the type of character (letter, digit, punctuation, etc.) at a specific index within a string.

Syntax

Following is the syntax −

public static System.Globalization.UnicodeCategory GetUnicodeCategory(string str, int index);

Parameters

  • str − The string to examine.
  • index − The zero-based position of the character to categorize within the string.

Return Value

Returns a UnicodeCategory enumeration value that identifies the group that contains the character at position index in str.

Examples

Categorizing a Lowercase Letter

Let us see an example to categorize a lowercase letter −

using System;
using System.Globalization;

public class Demo {
    public static void Main() {
        string val = "amit";
        UnicodeCategory unicode = Char.GetUnicodeCategory(val, 2);
        Console.WriteLine("Character at index 2: '" + val[2] + "'");
        Console.WriteLine("Unicode category: " + unicode);
    }
}

The output of the above code is −

Character at index 2: 'i'
Unicode category: LowercaseLetter

Categorizing a Decimal Digit

Let us see another example that categorizes a decimal digit −

using System;
using System.Globalization;

public class Demo {
    public static void Main() {
        string val = "hjk9878hj";
        UnicodeCategory unicode = Char.GetUnicodeCategory(val, 4);
        Console.WriteLine("Character at index 4: '" + val[4] + "'");
        Console.WriteLine("Unicode category: " + unicode);
    }
}

The output of the above code is −

Character at index 4: '8'
Unicode category: DecimalDigitNumber

Multiple Character Categories

Let us examine different types of characters in a single string −

using System;
using System.Globalization;

public class Demo {
    public static void Main() {
        string val = "Hello, World! 123";
        int[] indices = {0, 5, 7, 13, 14};
        
        foreach (int index in indices) {
            char character = val[index];
            UnicodeCategory category = Char.GetUnicodeCategory(val, index);
            Console.WriteLine($"Index {index}: '{character}' -> {category}");
        }
    }
}

The output of the above code is −

Index 0: 'H' -> UppercaseLetter
Index 5: ',' -> OtherPunctuation
Index 7: 'W' -> UppercaseLetter
Index 13: '!' -> OtherPunctuation
Index 14: ' ' -> SpaceSeparator

Common Unicode Categories

UnicodeCategory Description Example Characters
UppercaseLetter Uppercase letters A, B, C, Z
LowercaseLetter Lowercase letters a, b, c, z
DecimalDigitNumber Decimal digits 0, 1, 2, 9
SpaceSeparator Space characters Space, tab
OtherPunctuation Punctuation marks !, ?, ., ,

Conclusion

The Char.GetUnicodeCategory(String, Int32) method provides a convenient way to determine the Unicode category of any character within a string at a specified index. This method is particularly useful for text processing, validation, and character classification tasks in C# applications.

Updated on: 2026-03-17T07:04:35+05:30

270 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements