Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I convert a single char to a string?

I'd like to enumerate a string and instead of it returning chars I'd like to have the iterative variable be of type string. This probably isn't possible to have the iterative type be a string so what is the most efficient way to iterate through this string?

Do I need to create a new string object with each iteration of the loop or can I perform a cast somehow?

String myString = "Hello, World"; foreach (Char c in myString) {     // what I want to do in here is get a string representation of c     // but I can't cast expression of type 'char' to type 'string'     String cString = (String)c; // this will not compile } 
like image 624
Ian R. O'Brien Avatar asked Dec 06 '12 03:12

Ian R. O'Brien


People also ask

How do I convert a single character to a string in C++?

C++ c_str() function along with C++ String strcpy() function can be used to convert a string to char array easily. The c_str() method represents the sequence of characters in an array of string followed by a null character ('\0'). It returns a null pointer to the string.


1 Answers

Use the .ToString() Method

String myString = "Hello, World"; foreach (Char c in myString) {     String cString = c.ToString();  } 
like image 191
Mark Hall Avatar answered Oct 03 '22 11:10

Mark Hall