當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。