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


C# Char.Parse(String)用法及代碼示例

此方法用於將指定字符串的值轉換為其等效的Unicode字符。

用法:

public static char Parse (string s);

此處,s是包含單個字符或null的字符串。


返回值:此方法返回與s中的唯一字符等效的Unicode字符。

異常:

  • ArgumentNullException:如果s為null。
  • FormatException:如果s的長度不為1。

以下示例程序旨在說明Char.Parse(String)方法的用法:

示例1:

// C# program to demonstrate the  
// Char.Parse(String) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // calling Parse() Method 
            get("1"); 
            get("a"); 
            get("@"); 
            get("-"); 
        } 
        catch (ArgumentNullException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
        catch (FormatException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
  
    // Defining get() method 
    public static void get(string s) 
    { 
  
        // getting Unicode character 
        // using Parse() Method 
        char val = Char.Parse(s); 
  
        // display the char value 
        Console.WriteLine("Unicode character "+ 
               "of string {0} is {1}", s, val); 
    } 
}
輸出:
Unicode character of string 1 is 1
Unicode character of string a is a
Unicode character of string @ is @
Unicode character of string - is -

示例2:對於ArgumentNullException

// C# program to demonstrate the  
// Char.Parse(String) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // calling Parse() Method 
            get("1"); 
            get("a"); 
            get("@"); 
            get("-"); 
            Console.WriteLine(""); 
            Console.WriteLine("s is null"); 
            get(null); 
        } 
        catch (ArgumentNullException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
        catch (FormatException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
  
    // Defining get() method 
    public static void get(string s) 
    { 
  
        // getting Unicode character 
        // using Parse() Method 
        char val = Char.Parse(s); 
  
        // display the char value 
        Console.WriteLine("Unicode character of"+ 
                   " string {0} is {1}", s, val); 
    } 
}
輸出:
Unicode character of string 1 is 1
Unicode character of string a is a
Unicode character of string @ is @
Unicode character of string - is -

s is null
Exception Thrown: System.ArgumentNullException

示例3:對於FormatException

// C# program to demonstrate the  
// Char.Parse(String) Method 
using System; 
  
class GFG { 
  
    // Main Method 
    public static void Main() 
    { 
        try { 
  
            // calling Parse() Method 
            get("1"); 
            get("a"); 
            get("@"); 
            get("-"); 
            Console.WriteLine(""); 
            Console.WriteLine("The length of s is not 1."); 
            get("null"); 
        } 
        catch (ArgumentNullException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
        catch (FormatException e) { 
  
            Console.Write("Exception Thrown: "); 
            Console.Write("{0}", e.GetType(), e.Message); 
        } 
    } 
  
    // Defining get() method 
    public static void get(string s) 
    { 
  
        // getting Unicode character 
        // using Parse() Method 
        char val = Char.Parse(s); 
  
        // display the char value 
        Console.WriteLine("Unicode character of "+ 
                     "string {0} is {1}", s, val); 
    } 
}
輸出:
Unicode character of string 1 is 1
Unicode character of string a is a
Unicode character of string @ is @
Unicode character of string - is -

The length of s is not 1.
Exception Thrown: System.FormatException

參考:



相關用法


注:本文由純淨天空篩選整理自RohitPrasad3大神的英文原創作品 C# | Char.Parse(String) Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。