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:
csharp
// 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 GetUnicodeCategory() Method UnicodeCategory val = Char.GetUnicodeCategory(s, i); // Display the values Console.WriteLine("the letter present at "+ "index {0} is {1} ", i, val); } } |
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
csharp
// 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 GetUnicodeCategory() Method UnicodeCategory val = Char.GetUnicodeCategory(s, i); // Display the values Console.WriteLine("the letter present at "+ "index {0} is {1} ", i, val); } } |
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
csharp
// 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 GetUnicodeCategory() Method UnicodeCategory val = Char.GetUnicodeCategory(s, i); // Display the values Console.WriteLine("the letter present at "+ "index {0} is {1} ", i, val); } } |
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:
- https://docs.microsoft.com/en-us/dotnet/api/system.char.getunicodecategory?view=netframework-4.7.2#System_Char_GetUnicodeCategory_System_String_System_Int32_