Doubt with the C # language [closed]

-1

I have a String any type "chair".

Doubt

How do I make a space between all letters in C# ?

    
asked by anonymous 23.08.2018 / 12:51

2 answers

2

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

    
23.08.2018 / 12:58
2

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

    
23.08.2018 / 14:14