Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Char.Parse(String) Method in C#
The Char.Parse(String) method in C# is used to convert the value of the specified string to its equivalent Unicode character. This method is particularly useful when you need to extract a single character from a string representation.
Syntax
Following is the syntax −
public static char Parse(string s);
Parameters
-
s − A string that contains a single character, or null.
Return Value
Returns a Unicode character that is equivalent to the sole character in s.
Examples
Example 1: Parsing a Letter Character
The following example demonstrates parsing a single letter character −
using System;
public class Demo {
public static void Main() {
string str = "a";
char res = Char.Parse(str);
Console.WriteLine("Value = " + str);
Console.WriteLine("Equivalent Unicode character = " + res);
Console.WriteLine("Unicode value: " + (int)res);
}
}
The output of the above code is −
Value = a Equivalent Unicode character = a Unicode value: 97
Example 2: Parsing a Symbol Character
The following example demonstrates parsing a special symbol character −
using System;
public class Demo {
public static void Main() {
string str = "#";
char res = Char.Parse(str);
Console.WriteLine("Value = " + str);
Console.WriteLine("Equivalent Unicode character = " + res);
Console.WriteLine("Unicode value: " + (int)res);
}
}
The output of the above code is −
Value = # Equivalent Unicode character = # Unicode value: 35
Example 3: Exception Handling
The Char.Parse() method throws exceptions for invalid input. Here's how to handle common scenarios −
using System;
public class Demo {
public static void Main() {
try {
// Valid single character
char valid = Char.Parse("5");
Console.WriteLine("Parsed character: " + valid);
// This will throw ArgumentNullException
// char invalid1 = Char.Parse(null);
// This will throw FormatException
// char invalid2 = Char.Parse("ab");
Console.WriteLine("Parsing completed successfully");
}
catch (Exception ex) {
Console.WriteLine("Exception: " + ex.Message);
}
}
}
The output of the above code is −
Parsed character: 5 Parsing completed successfully
Common Use Cases
-
Converting single-character strings from user input or configuration files
-
Processing individual characters from formatted data
-
Validating that a string contains exactly one character
Key Rules
-
The string must contain exactly one character
-
Throws
ArgumentNullExceptionif the string is null -
Throws
FormatExceptionif the string is empty or contains more than one character
Conclusion
The Char.Parse() method provides a straightforward way to convert single-character strings to char values. It's essential to handle potential exceptions when the input doesn't contain exactly one character, making it a reliable tool for character conversion operations.
