此方法用于将指定字符串指定位置的字符分类为由UnicodeCategory值之一标识的组。
句法:
public static System.Globalization.UnicodeCategory GetUnicodeCategory (string s, int index);
参数:
- s :是弦乐。
- index:是s中的字符位置。
返回值:该方法返回一个UnicodeCategory枚举常量,该常量标识在s中的位置索引处包含字符的组。
例外情况:
- ArgumentNullException :如果s为null。
- ArgumentOutOfRangeException :如果索引小于零或大于s中的最后一个位置。
下面的程序说明了Char.GetUnicodeCategory(String,Int32)方法的用法:
范例1:
// 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);
}
}
输出:
the letter present at index 3 is DecimalDigitNumber
the letter present at index 0 is UppercaseLetter
the letter present at index 2 is LowercaseLetter
示例2:对于ArgumentNullException
// 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);
}
}
输出:
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
示例3:对于ArgumentOutOfRangeException
// 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);
}
}
输出:
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
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.char.getunicodecategory?view=netframework-4.7.2#System_Char_GetUnicodeCategory_System_String_System_Int32_