jeudi 4 octobre 2018

How to split a string if the delimiter is one or more spaces?

I'm trying to get this to write every word from input in a new line, even if there are more spaces between words, but I can't figure out what is wrong with this.

string phrase = Console.ReadLine();
string currentWord = ""; 

for (int i = 0; i < phrase.Length; i++)
{
if (phrase[i] == ' ') 
    Console.WriteLine(currentWord);
currentWord = "";
while (phrase[i] == ' ') 
    i++; 
if (phrase[i] != ' ') 
    currentWord += phrase[i];
}
Console.WriteLine(currentWord);

I'm only getting the last letter from every word. Any help, please?

Aucun commentaire:

Enregistrer un commentaire