Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - How to convert string to char?

I am a beginner in C# and I would like to know how to convert strings to chars, specifically string[] to char[]. I tried ToCharArray(), but I then I got an error saying that it doesn't exist. Convert.ToChar(<char here>) gives me a error saying

cannot convert from "char" to "System.Array"

like image 582
enricogp Avatar asked Nov 26 '15 20:11

enricogp


1 Answers

Use:

string str = "Hello"; char[] characters = str.ToCharArray(); 

If you have a single character string, You can also try

string str = "A"; char character = char.Parse(str);  //OR  string str = "A"; char character = str.ToCharArray()[0]; 
like image 65
Junaid Pathan Avatar answered Oct 02 '22 00:10

Junaid Pathan