How do I convert a single char to a string?

C#StringCastingChar

C# Problem Overview


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
}

C# Solutions


Solution 1 - C#

Use the .ToString() Method

String myString = "Hello, World";
foreach (Char c in myString)
{
    String cString = c.ToString(); 
}

Solution 2 - C#

You have two options. Create a string object or call ToString method.

String cString = c.ToString();
String cString2 = new String(c, 1); // second parameter indicates
                                    // how many times it should be repeated

Solution 3 - C#

With C# 6 interpolation:

char ch = 'A';
string s = $"{ch}";

This shaves a few bytes. :)

Solution 4 - C#

It seems that the obvious thing to do is this:

String cString = c.ToString()

Solution 5 - C#

Create a new string from the char.

 String cString = new String(new char[] { c });

or

 String cString = c.ToString();

Solution 6 - C#

Create an extension method:

public static IEnumerable<string> GetCharsAsStrings(this string value)
{
    return value.Select(c =>
           {
                //not good at all, but also a working variant
                //return string.Concat(c);

                return c.ToString();
           });
}

and loop through strings:

string s = "123456";
foreach (string c in s.GetCharsAsStrings())
{
    //...
}

Solution 7 - C#

Did you try:

String s = new String(new char[] { 'c' });

Solution 8 - C#

String cString = c.ToString();

Solution 9 - C#

Why not this code? Won't it be faster?

string myString = "Hello, World";
foreach( char c in myString )
{
    string cString = new string( c, 1 );
}

Solution 10 - C#

> probably isn't possible to have the iterative type be a string

Sure it is:

foreach (string str in myString.Select(c => c.ToString())
{
...
}

Any of the suggestions in the other answers can be substituted for c.ToString(). Probably the most efficient by a small hair is c => new string(c, 1), which is what char.ToString() probably does under the hood.

Solution 11 - C#

you can use + with empty string "", please check the below code:

char a = 'A';
//a_str is a string, the value of which is "A".
string a_str = ""+a;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIan R. O&#39;BrienView Question on Stackoverflow
Solution 1 - C#Mark HallView Answer on Stackoverflow
Solution 2 - C#Lukasz MadonView Answer on Stackoverflow
Solution 3 - C#seanView Answer on Stackoverflow
Solution 4 - C#Ian R. O'BrienView Answer on Stackoverflow
Solution 5 - C#Michael GView Answer on Stackoverflow
Solution 6 - C#horghView Answer on Stackoverflow
Solution 7 - C#trebuchetView Answer on Stackoverflow
Solution 8 - C#JeroenView Answer on Stackoverflow
Solution 9 - C#user1489240View Answer on Stackoverflow
Solution 10 - C#Jim BalterView Answer on Stackoverflow
Solution 11 - C#AlexView Answer on Stackoverflow