Convert a Character to the String in C#
Given a character, the task is to character to the string in C#.
Examples:
Input : X = 'a' Output : string S = "a" Input : X = 'A' Output : string S = "A"
Approach: The idea is to use ToString() method, the argument is the character and it returns string converting Unicode character to the string.
// convert the character x // to string s public string ToString(IFormatProvider provider);
C#
// C# program to character to the string using System; public class GFG{ static string getString( char x) { // Char.ToString() is a System.Char // struct method which is used // to convert the value of this // instance to its equivalent // string representation string str = x.ToString(); return str; } static void Main( string [] args) { char chr = 'A' ; Console.WriteLine( "Type of " + chr + " : " + chr.GetType()); string str = getString(chr); Console.WriteLine( "Type of " + str + " : " + str.GetType()); } } |
Output:
Type of A : System.Char Type of A : System.String
Contact Us