此方法用于将字符串指定位置的UTF-16编码字符或代理对的值转换为Unicode代码点。
句法:
public static int ConvertToUtf32 (string s, int index);
参数:
s: A string that contains a character or surrogate pair.
index: The index position of the character or surrogate pair in s.
返回值:此方法返回由index所指定的s参数中的字符或代理对表示的21位Unicode代码点。
例外情况:
- ArgumentNullException:如果s为null。
- ArgumentOutOfRangeException:如果index不在s内。
- 参数异常:如果索引不是s内的位置。
- ArgumentException:如果指定的索引位置包含一个代理对,并且该对中的第一个字符不是有效的高代理,或者该对中的第二个字符不是有效的低代理。
下面的程序说明了Char.ConvertToUtf32(String,Int32)方法的用法:
范例1:
// C# program to demonstrate
// Char.ConvertToUtf32(String, Int32)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// declaring and initializing int
// variable with 21 bit unicode
int utf = 0xF42;
// getting the value
// using ConvertFromUtf32()
string value = Char.ConvertFromUtf32(utf);
// getting unicode point
// using ConvertToUtf32() method
int val = Char.ConvertToUtf32(value, 0);
// Display the value
Console.WriteLine("value is 0x{0:X}", val);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
value is 0xF42
示例2:对于ArgumentOutOfRangeException
// C# program to demonstrate
// Char.ConvertToUtf32(String, Int32)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// declaring and initializing int
// variable with 21 bit unicode
int utf = 0x42F;
// getting the value
// using ConvertFromUtf32()
string value = Char.ConvertFromUtf32(utf);
// getting unicode point
// using ConvertToUtf32() method
Console.WriteLine("index is not a position within s.");
int val = Char.ConvertToUtf32(value, 0xDFFF);
// Display the value
Console.WriteLine("value is 0x{0:X}", val);
}
catch (ArgumentOutOfRangeException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
index is not a position within s.
Exception Thrown: System.ArgumentOutOfRangeException
示例3:对于ArgumentNullException
// C# program to demonstrate
// Char.ConvertToUtf32(String, Int32)
// Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// declaring and initializing
// int variablewith 21 bit
// unicode int utf = 0x42F;
// getting the value
// using ConvertFromUtf32()
string value = null;
// getting unicode point
// using ConvertToUtf32() method
Console.WriteLine("string value is null");
int val = Char.ConvertToUtf32(value, 0);
// Display the value
Console.WriteLine("value is 0x{0:X}", val);
}
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);
}
catch (ArgumentException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
}
输出:
string value is null
Exception Thrown: System.ArgumentNullException
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.char.converttoutf32?view=netframework-4.7.2#System_Char_ConvertToUtf32_System_String_System_Int32_