Convert a char to a string in C#
This article illustrates the different techniques to convert a char to a string in C#.
1. Using Char.ToString() method
To convert a single char to a string in C#, you can use the ToString() method. It converts the current char value to its string equivalent, as the following code example illustrates.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { char ch = 'A'; string str = ch.ToString(); Console.WriteLine(str); // A } } |
2. Using String Constructor
You can also create a new string instance using the string constructor. The specified value is repeated a certain number of times, as supplied in the second parameter.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { char ch = 'A'; string str = new String(ch, 1); Console.WriteLine(str); // A } } |
Alternatively, you can construct a character array from the given character and create a new String instance from it.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { char ch = 'A'; string str = new String(new char[] { ch }); Console.WriteLine(str); // A } } |
3. Using $ (String Interpolation)
If you’re using the C# 6, consider using the interpolated string. An interpolated string is identified using the $ special character leading the string literal, and provides a convenient syntax to format strings.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System; public class Example { public static void Main() { char ch = 'A'; string str = $"{ch}"; Console.WriteLine(str); // A } } |
That’s all about converting a char to a string 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 :)