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


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。