此方法用于使用指定的区域性特定格式信息将指定对象的值转换为其等效的Unicode字符。
句法:
public static char ToChar (object value, IFormatProvider provider);
参数:
- value :它是一个长度为1或为null的字符串。
- provider :提供者,它提供特定于区域性的格式信息。
返回值:该方法返回一个Unicode字符,它等效于value中的第一个也是唯一的字符。
例外情况:
- ArgumentNullException :如果该值为null。
- FormatException :如果value的长度不为1。
下面的程序说明了Convert.ToChar(String,IFormatProvider)方法的用法:
范例1:
// C# program to demonstrate the
// Convert.ToChar() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and initializing
// String array
string[] values = {"A", "B", "a",
"b", "x", "z"};
// calling get() Method
Console.WriteLine("Converted char value "+
"of specified strings: ");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
}
catch (FormatException e) {
Console.WriteLine("\n");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (OverflowException e) {
Console.WriteLine("\n");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s,
CultureInfo cultures)
{
// converting string to specified char
char val = Convert.ToChar(s, cultures);
// display the converted char value
Console.Write(" {0}, ", val);
}
}
输出:
Converted char value of specified strings:
A, B, a, b, x, z,
示例2:对于ArgumentNullException
// C# program to demonstrate the
// Convert.ToChar() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and initializing String array
string[] values = {"A", "B", "a",
"b", "x", "z"};
// calling get() Method
Console.WriteLine("Converted char value "+
"of specified strings: ");
for (int j = 0; j < values.Length; j++) {
get(values[j], cultures);
}
Console.WriteLine("\n");
string s = null;
Console.WriteLine("s is null ");
// converting string to specified char
char val = Convert.ToChar(s, cultures);
// display the converted char value
Console.Write(" {0}, ", val);
}
catch (FormatException e) {
Console.WriteLine("\n");
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s, CultureInfo cultures)
{
// converting string to specified char
char val = Convert.ToChar(s, cultures);
// display the converted char value
Console.Write(" {0}, ", val);
}
}
输出:
Converted char value of specified strings:
A, B, a, b, x, z,
s is null
Exception Thrown: System.ArgumentNullException
示例3:对于FormatException
// C# program to demonstrate the
// Convert.ToChar() Method
using System;
using System.Globalization;
class GFG {
// Main Method
public static void Main()
{
try {
// creating object of CultureInfo
CultureInfo cultures = new CultureInfo("en-US");
// declaring and initializing String array
string value1 = "x";
string value2 = "xyz";
get(value1, cultures);
Console.WriteLine("\nlength of value2 is not 1");
get(value2, cultures);
}
catch (FormatException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
catch (ArgumentNullException e) {
Console.Write("Exception Thrown: ");
Console.Write("{0}", e.GetType(), e.Message);
}
}
// Defining get() method
public static void get(string s,
CultureInfo cultures)
{
// converting string to specified char
char val = Convert.ToChar(s, cultures);
// display the converted char value
Console.WriteLine("string to char value : {0} ", val);
}
}
输出:
string to char value : x
length of value2 is not 1
Exception Thrown: System.FormatException
参考:
- https://docs.microsoft.com/zh-cn/dotnet/api/system.convert.tochar?view=netframework-4.7.2#System_Convert_ToChar_System_String_System_IFormatProvider_