Reputation: 6930
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
}
Upvotes: 72
Views: 125359
Reputation: 8596
I recently wanted to know which approach was the fastest, so I benchmarked 5 approaches.
The answer is that you should use new String(myChar,1)
, for the fastest times:
| Method | Mean | Error | StdDev | Rank | Gen0 | Allocated |
|----------------- |----------:|---------:|----------:|-----:|-------:|----------:|
| NewString | 10.65 ns | 0.502 ns | 1.473 ns | 1 | 0.0031 | 16 B |
| AddString | 11.83 ns | 0.845 ns | 2.466 ns | 2 | 0.0030 | 16 B |
| CharToString | 12.26 ns | 0.662 ns | 1.951 ns | 2 | 0.0030 | 16 B |
| NewStringPointer | 33.03 ns | 1.382 ns | 3.988 ns | 3 | 0.0030 | 16 B |
| Interpolate | 119.31 ns | 5.351 ns | 15.525 ns | 4 | 0.0083 | 44 B |
Where the 5 methods are:
public string CharToString(char input) => input.ToString();
public string Interpolate(char input) => $"{input}";
public string AddString(char input) => input + "";
public string NewString(char input) => new String(input,1);
public string NewStringPointer(char input)
{
unsafe
{
return new String(& input);
}
}
Upvotes: 3
Reputation: 649
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;
Upvotes: 1
Reputation: 16424
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.
Upvotes: 0
Reputation: 917
With C# 6 interpolation:
char ch = 'A';
string s = $"{ch}";
This shaves a few bytes. :)
Upvotes: 8
Reputation: 31
Why not this code? Won't it be faster?
string myString = "Hello, World";
foreach( char c in myString )
{
string cString = new string( c, 1 );
}
Upvotes: 0
Reputation: 18563
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())
{
//...
}
Upvotes: 1
Reputation: 15004
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
Upvotes: 15
Reputation: 54562
Use the .ToString() Method
String myString = "Hello, World";
foreach (Char c in myString)
{
String cString = c.ToString();
}
Upvotes: 90
Reputation: 6755
Create a new string from the char.
String cString = new String(new char[] { c });
or
String cString = c.ToString();
Upvotes: 4
Reputation: 6930
It seems that the obvious thing to do is this:
String cString = c.ToString()
Upvotes: 5