计数在C#中的单词数 [英] Counting number of words in C#

查看:115
本文介绍了计数在C#中的单词数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个格式文本框计算单词的数量在C#中,我有以下如果它是一个单行只能代码。我如何做到这一点不依赖于正则表达式或其他特殊功能。

 字符串whole_text = richTextBox1.Text; 
串trimmed_text = whole_text.Trim();
的String [] = split_text trimmed_text.Split('');
INT space_count = 0;
串NEW_TEXT =;

的foreach(在split_text串AV)
{
如果(AV ==)
{
space_count ++;
}
,否则
{
NEW_TEXT = NEW_TEXT + AV +,;
}
}

NEW_TEXT = new_text.TrimEnd(,);
split_text = new_text.Split(,);
MessageBox.Show(split_text.Length.ToString());


解决方案

由于您只对字数感兴趣,然后你不关心个人的话, String.Split 可以避免的。 String.Split 是很方便,但它不必要产生一个(潜在的)大量字符串的对象,这反过来又创造垃圾收集器带来不必要的负担。对于文本的每个字,需要被实例化,然后因为你不使用它很快就收集到一个新的字符串对象。



有关家庭作业,这可能没有关系,但如果你的文本框的内容经常变化,你在事件处理中这样的计算,它可能是明智的,通过简单的手动字符迭代。如果你真的想使用 String.Split ,然后去如简化版本。Yonix 的建议



另外,使用类似这样的算法:

 字符串文本= richTextBox1.Text.Trim(); 
INT的wordCount = 0,索引= 0;

,而(指数< text.Length)
{
//检查是否当前字符是单词
的一部分,而(指数< text.Length&放;&安培; Char.IsWhiteSpace(文[指数])==假)
指数++;

++的wordCount;

//跳过空白,直到下一个单词
,而(指数< text.Length&放大器;&安培; Char.IsWhiteSpace(文[指数])==真)
指数++;
}

此代码应当与在那里你有每个单词之间的多个空格的情况下更好地工作。


I'm trying to count the number of words from a rich textbox in C# the code that I have below only works if it is a single line. How do I do this without relying on regex or any other special functions.

string whole_text = richTextBox1.Text;
string trimmed_text = whole_text.Trim();
string[] split_text = trimmed_text.Split(' ');
int space_count = 0;
string new_text = "";

foreach(string av in split_text)
{
    if (av == "")
    {
        space_count++;
    }
    else 
    { 
        new_text = new_text  + av + ",";
    }
}

new_text = new_text.TrimEnd(',');
split_text = new_text.Split(',');
MessageBox.Show(split_text.Length.ToString ());

解决方案

Since you are only interested in word count, and you don't care about individual words, String.Split could be avoided. String.Split is handy, but it unnecessarily generates a (potentially) large number of String objects, which in turn creates an unnecessary burden on the garbage collector. For each word in your text, a new String object needs to be instantiated, and then soon collected since you are not using it.

For a homework assignment, this may not matter, but if your text box contents change often and you do this calculation inside an event handler, it may be wiser to simply iterate through characters manually. If you really want to use String.Split, then go for a simpler version like Yonix recommended.

Otherwise, use an algorithm similar to this:

String text = richTextBox1.Text.Trim();
int wordCount = 0, index = 0;

while (index < text.Length)
{
   // check if current char is part of a word
   while (index < text.Length && Char.IsWhiteSpace(text[index]) == false)
       index++;

   wordCount++;

   // skip whitespace until next word
   while (index < text.Length && Char.IsWhiteSpace(text[index]) == true)
       index++;
}

This code should work better with cases where you have multiple spaces between each word.

这篇关于计数在C#中的单词数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆