![](/img/trans.png)
[英]In c# richtextbox how can i highlight words in a sentence sequentially.
[英]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();
}
现在,我怎么能数数呢? 句子中的单词?
一个简单的方法就是使用Split
并获取结果数组的Length
根据数组中的字符将字符串拆分为子字符串。 您可以指定子字符串是否包含空数组元素。
string sentence = Console.ReadLine();
var array= sentence.Split(new []{' '},StringSplitOptions.RemoveEmptyEntries);
Console.WriteLine(array.Length);
但是,这取决于您将什么归类为单词。 即是一个数字一个词?。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.