Convert a Character to the String in C#
Last Updated :
26 May, 2020
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#
using System;
public class GFG{
static string getString( char x)
{
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
Share your thoughts in the comments
Please Login to comment...