[英]How can I count the number of words in a sentence using c#? I am just starting to learn this language
{
// This program counts the number of words in a sentence
Console.WriteLine("Write a sentence.");
string sentence = Console.ReadLine();
}
Now, how can i count the no.现在,我怎么能数数呢? of words in the sentence?句子中的单词?
A simple approach would just to use Split
and get the Length
of the resulting array一个简单的方法就是使用Split
并获取结果数组的Length
Splits a string into substrings based on the characters in an array.根据数组中的字符将字符串拆分为子字符串。 You can specify whether the substrings include empty array elements.您可以指定子字符串是否包含空数组元素。
string sentence = Console.ReadLine();
var array= sentence.Split(new []{' '},StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(array.Length);
However it depends what you class as word.但是,这取决于您将什么归类为单词。 Ie is a number a word?.即是一个数字一个词?。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.