how to convert string to char or vice versa  
Author Message
yeos_lee





PostPosted: Visual C# Language, how to convert string to char or vice versa Top

Hi expert,
what c# code that i can use to convert char to string or vice versa

thanks in advance.



Visual C#20  
 
 
Prasant Swain





PostPosted: Visual C# Language, how to convert string to char or vice versa Top

You can use the convert function to convert it.

Convert.ToString(<character>);

Convert.ToChar(<string>);



 
 
Matthew Watson





PostPosted: Visual C# Language, how to convert string to char or vice versa Top

Alternatively you can do:

char myChar = 'X';

string myString = new string(myChar,1);
or
string myString = myChar.ToString()

and

char myChar = myString[0];

(If you convert a string to a char, you have to choose one of its characters to convert, via array indexing. Using Convert.ToChar(string) always uses the first char in the string.)





 
 
ahmedilyas





PostPosted: Visual C# Language, how to convert string to char or vice versa Top

you can use this to convert string to a char:

char[] myChar = theString.ToCharArray();