Convert a string to a char in C#
This article illustrates the different techniques to convert a string to a char in C#.
To convert a single character string to a char, you can use the char.Parse()
method. It converts the value of the specified single-character string to its Unicode character equivalent.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { string s = "a"; char ch = char.Parse(s); Console.WriteLine(ch); // a } } |
If the string is a multi-character string, you can use the string.ToCharArray()
method to get a Unicode character array from the characters in the string instance.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { string s = "abc"; char[] chars = s.ToCharArray(); Console.WriteLine(String.Join(", ", chars)); // a, b, c } } |
That’s all about converting a string to a char in C#.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)