各文に含まれる単語の数を数える方法にこだわっています。この例は次のとおりです。次の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());
しかし、各文にいくつの単語が含まれているか数えることはできません。