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
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
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);
}
}
public static void check( string s, int i)
{
UnicodeCategory val = Char.GetUnicodeCategory(s, i);
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
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
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);
}
}
public static void check( string s, int i)
{
UnicodeCategory val = Char.GetUnicodeCategory(s, i);
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
using System;
using System.Globalization;
class GFG {
public static void Main()
{
try {
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);
}
}
public static void check( string s, int i)
{
UnicodeCategory val = Char.GetUnicodeCategory(s, i);
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:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!