User input then split string

前端 未结 2 389
野性不改
野性不改 2021-01-27 05:55

I have looked online for many solutions to my problem. I want to ask the user to input a sentence and write the sentence out one word per line using the split method. I have ask

相关标签:
2条回答
  • 2021-01-27 06:28

    You are currently splitting by a comma , only when you need to split by punctuations that may exist in a sentence like space " " comma "," and peroid "."

    //..other code
    string[] split = sentenceTwo.Split(new char[]{' ', '.', ','}, System.StringSplitOptions.RemoveEmptyEntries);
    foreach (string item in split)
    {
        Console.WriteLine(item);
    }
    //..other code
    
    0 讨论(0)
  • 2021-01-27 06:38

    You should split the string on space instead of on comma:

    namespace Seperation
    {
        class Program
        {
            static void Main()
            {
                string temp;
                string sentenceTwo = (" ");
    
                Console.WriteLine("please enter a sentence");
                temp = Console.ReadLine();
                sentenceTwo = temp;
    
                string[] split = sentenceTwo.Split(' ');
                foreach (string item in split)
                {
                    Console.WriteLine(item);
                }
                Console.ReadLine();        
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题