Este método se utiliza para convertir el valor de la string especificada en su carácter Unicode equivalente.
Sintaxis:
public static char Parse (string s);
Aquí, s es una string que contiene un solo carácter o nulo.
Valor de retorno: este método devuelve un carácter Unicode equivalente al carácter único en s .
Excepciones:
ArgumentNullException: si s es nulo.
FormatException: si la longitud de s no es 1.
Los siguientes programas ilustran el uso del método Char.Parse(String):
Ejemplo 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); } }
Producción:
Unicode character of string 1 is 1 Unicode character of string a is a Unicode character of string @ is @ Unicode character of string - is -
Ejemplo 2: para 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); } }
Producción:
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
Ejemplo 3: para 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); } }
Producción:
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
Referencia:
Publicación traducida automáticamente
Artículo escrito por RohitPrasad3 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA