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

This method is used to categorizes the character at the specified position in a specified string into a group identified by one of the UnicodeCategory values.

Syntax:

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

Parameters:

  • s: It is the String.
  • index: It is the character position in s.

Return Value: This method returns a UnicodeCategory enumerated constant that identifies the group that contains the character at position index in s.

Exceptions:

  • ArgumentNullException: If the s is null.
  • ArgumentOutOfRangeException: If the index is less than zero or greater than the last position in s.

Below programs illustrate the use of Char.GetUnicodeCategory(String, Int32) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Char.GetUnicodeCategory(String,
// Int32) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // calling check() Method
            check("1234", 3);
            check("Tsunami", 0);
            check("psyc0lo", 2);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
  
    // Defining check() method
    public static void check(string s, int i)
    {
  
        // checking condition
        // using GetNumericValue() Method
        UnicodeCategory val = Char.GetUnicodeCategory(s, i);
  
        // Display the values
        Console.WriteLine("the letter present at "+
                      "index {0} is {1} ", i, val);
    }
}

chevron_right


Output:

the letter present at index 3 is DecimalDigitNumber 
the letter present at index 0 is UppercaseLetter 
the letter present at index 2 is LowercaseLetter

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Char.GetUnicodeCategory(String,
// Int32) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // calling check() Method
            check("1234", 3);
            check("Tsunami", 0);
            check("psyc0lo", 2);
            Console.WriteLine("");
            Console.WriteLine("s is null");
            check(null, 2);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
    // Defining check() method
    public static void check(string s, int i)
    {
        // checking condition
        // using GetNumericValue() Method
        UnicodeCategory val = Char.GetUnicodeCategory(s, i);
  
        // Display the values
        Console.WriteLine("the letter present at "+
                      "index {0} is {1} ", i, val);
    }
}

chevron_right


Output:

the letter present at index 3 is DecimalDigitNumber 
the letter present at index 0 is UppercaseLetter 
the letter present at index 2 is LowercaseLetter 

s is null
Exception Thrown: System.ArgumentNullException

Example 3: For ArgumentOutOfRangeException

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Char.GetUnicodeCategory(String,
// Int32) Method
using System;
using System.Globalization;
  
class GFG {
  
    // Main Method
    public static void Main()
    {
        try {
  
            // calling check() Method
            check("1234", 3);
            check("Tsunami", 0);
            check("psyc0lo", 2);
            Console.WriteLine("");
            Console.WriteLine("index is less than zero");
            check("null", -1);
        }
        catch (ArgumentNullException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
        catch (ArgumentOutOfRangeException e) {
  
            Console.Write("Exception Thrown: ");
            Console.Write("{0}", e.GetType(), e.Message);
        }
    }
    // Defining check() method
    public static void check(string s, int i)
    {
        // checking condition
        // using GetNumericValue() Method
        UnicodeCategory val = Char.GetUnicodeCategory(s, i);
  
        // Display the values
        Console.WriteLine("the letter present at "+
                      "index {0} is {1} ", i, val);
    }
}

chevron_right


Output:

the letter present at index 3 is DecimalDigitNumber 
the letter present at index 0 is UppercaseLetter 
the letter present at index 2 is LowercaseLetter 

index is less than zero
Exception Thrown: System.ArgumentOutOfRangeException

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.