当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C# Convert.ToChar(String, IFormatProvider)用法及代码示例


此方法用于使用指定的区域性特定格式信息将指定对象的值转换为其等效的Unicode字符。

用法:

public static char ToChar (object value, IFormatProvider provider);

参数:


  • value:它是一个长度为1或为null的字符串。
  • provider:它是提供特定于区域性格式信息的对象。

返回值:此方法返回一个Unicode字符,它等效于值中的第一个也是唯一的字符。

异常:

  • ArgumentNullException:如果该值为空。
  • FormatException:如果值的长度不是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

参考:



相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 C# | Convert.ToChar(String, IFormatProvider) Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。