I have a String
any type "chair".
Doubt
How do I make a space between all letters in C#
?
I have a String
any type "chair".
How do I make a space between all letters in C#
?
You can do it this way, make sure this is what you need.
string palavra = "palavra";
string nova_palavra = "";
foreach (var c in palavra)
{
nova_palavra += c + " ";
}
nova_palavra = nova_palavra.Remove(nova_palavra.Length - 1);
Console.WriteLine(nova_palavra);
See the example by running here: link
There is another way to do the same:
string palavra = "palavra";
string palavaComEspacos = string.Join(" ", palavra.ToCharArray());
Console.WriteLine(palavaComEspacos);
In this solution, the result is exactly what you are looking for, the ToCharArray method returns an array of all the characters in the string, and the string.Join joins this array using a space (first parameter).
In addition, Join uses the StringBuilder, which avoids overloading the Garbage Colletor, you can find the implementation of the method in the official documentation: link
DotNetFiddle: link