How to convert a character to a string in C#
Overview
We can convert a character to a string in C# using the ToString() method. It converts a character value to its equivalent string representation.
Syntax
public static string ToString (char c);
Parameters
c: The character we want to change to string.
Return Value
The value returned is a string representation of the specified character.
Example
We will convert some characters to strings in the code below using the ToString() method:
// use Systemusing System;// create classclass HelloWorld{// main methodstatic void Main(){// create characterschar c1 = 'Y';char c2 = 'E';char c3 = 'S';// conver to stringsstring s1 = Char.ToString(c1);string s2 = Char.ToString(c2);string s3 = Char.ToString(c3);// print returned valueConsole.WriteLine(s1);Console.WriteLine(s2);Console.WriteLine(s3);}}
Explanation
From the code above, we convert the characters to strings using the ToString() method.
- In
line 10toline 12we create some character variables and create initials for them. - In
line 15toline 17we convert the characters to strings using theToString()method. - From
line 20toline 22we print the results.