📜  C#| Char.Parse(String)方法

📅  最后修改于: 2021-05-29 18:43:42             🧑  作者: Mango

此方法用于将指定字符串的值转换为其等效的Unicode字符。

句法:

public static char Parse (string s);

此处, s是包含单个字符或null的字符串。

返回值:该方法返回一个Unicode字符等效于s中的唯一字符。

例外情况:

下面的程序说明了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

参考:

  • https://docs.microsoft.com/zh-cn/dotnet/api/system.char.parse?view=netframework-4.7.2