1

我被困在如何计算每个句子中有多少单词,一个例子是:string sentence = "hello how are you. I am good. that's good." 并让它像这样出现:

//sentence1: 4 words
//sentence2: 3 words
//sentence3: 2 words

我可以得到句子的数量

    public int GetNoOfWords(string s)
    {
        return s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length;
    }
    label2.Text = (GetNoOfWords(sentance).ToString());

我可以得到整个字符串中的单词数

    public int CountWord (string text)
    {
        int count = 0;
        for (int i = 0; i < text.Length; i++)
        {
            if (text[i] != ' ')
            {
                if ((i + 1) == text.Length)
                {
                    count++;
                }
                else
                {
                    if(text[i + 1] == ' ')
                    {
                        count++;
                    }
                }
            } 
        }
        return count;
    }

然后按钮1

        int words = CountWord(sentance);
        label4.Text = (words.ToString());

我数不清每句话有多少个单词。

4

8 回答 8

5

而不是像你在CountWords我做的那样循环字符串;

 int words = s.Split(' ').Length;

它更加干净和简单。您在空白处拆分,它返回所有单词的数组,该数组的长度是字符串中的单词数。

于 2012-11-21T03:27:42.027 回答
4

为什么不使用拆分呢?

        var sentences = "hello how are you. I am good. that's good.";

        foreach (var sentence in sentences.TrimEnd('.').Split('.'))
            Console.WriteLine(sentence.Trim().Split(' ').Count());
于 2012-11-21T03:28:57.463 回答
1

如果你想要每个句子中的单词数,你需要

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 + "*");
}
于 2012-11-21T03:30:25.597 回答
1

对 s.Split 返回的数组的每个元素使用 CountWord:

string sentence = "hello how are you. I am good. that's good.";
string[] words = sentence.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).Length;

for (string sentence in sentences)
{
    int noOfWordsInSentence = CountWord(sentence);
}
于 2012-11-21T03:32:14.693 回答
1
string text = "hello how are you. I am good. that's good.";
string[] sentences = s.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
IEnumerable<int> wordsPerSentence = sentences.Select(s => s.Trim().Split(' ').Length);
于 2012-11-21T03:33:23.060 回答
1

正如此处的几个答案中所述,请查看拆分、修剪、替换等字符串函数以帮助您进行操作。这里的所有答案都将解决您的简单示例,但这里有一些句子可能无法正确分析;

"Hello, how are you?" (no '.' to parse on)
"That apple costs $1.50."  (a '.' used as a decimal)
"I   like     whitespace    .    "   
"Word"  
于 2012-11-21T03:34:50.517 回答
1

如果您只需要计数,我会避免Split()- 它占用了不必要的空间。也许:

static int WordCount(string s)
{
    int wordCount = 0;
    for(int i = 0; i < s.Length - 1; i++)
        if (Char.IsWhiteSpace(s[i]) && !Char.IsWhiteSpace(s[i + 1]) && i > 0)
            wordCount++;
    return ++wordCount;
}

public static void Main()
{
    Console.WriteLine(WordCount(" H elloWor    ld g  ")); // prints "4"
}

它根据空格数(1 个空格 = 2 个单词)计算。连续的空格被忽略。

于 2020-06-13T10:08:18.027 回答
-1

你的句子拼写是:

int words = CountWord(sentance);

有什么关系吗?

于 2012-11-21T03:30:46.160 回答