Count how many words in each sentence

后端 未结 8 2152
耶瑟儿~
耶瑟儿~ 2021-01-17 05:55

I\'m stuck on how to count how many words are in each sentence, an example of this is: string sentence = \"hello how are you. I am good. that\'s good.\" and ha

8条回答
  •  没有蜡笔的小新
    2021-01-17 06:24

    If you want number of words in each sentence, you need to

    string s = "This is a sentence. Also this counts. This one is also a thing.";
    string[] sentences = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
    foreach(string sentence in sentences)
    {
        Console.WriteLine(sentence.Split(' ').Length + " words in sentence *" + sentence + "*");
    }
    

提交回复
热议问题