C# | Char.Parse(String) Method

This method is used to convert the value of the specified string to its equivalent Unicode character.

Syntax:

public static char Parse (string s);

Here, s is a string that contains a single character, or null.



Return Value: This method returns a Unicode character equivalent to the sole character in s.

Exceptions:

ArgumentNullException: If s is null.

FormatException: If the length of s is not 1.

Below programs illustrate the use of Char.Parse(String) Method:

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

Unicode character of string 1 is 1
Unicode character of string a is a
Unicode character of string @ is @
Unicode character of string - is -

Example 2: For ArgumentNullException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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

Example 3: For FormatException

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


Output:

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

Reference:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.