首页  ·  知识 ·  编程语言
C#字数统计(wordcount)
网友    .NET  编辑:dezai   图片来源:网络
C# 字数统计(word count)
1、           Regex.Matches(s, @"[A-Za-z0-9'-.]+").Count
 
2、           string[] str=FilterHtml(content).Split("~!@#$%^&*()_+-=`|\\:\"?><;',./ ".ToCharArray());
 
                int i=0;
 
                foreach (string s in str)
 
                {
 
                    if (s.Length == 0)
 
                        i++;
 
                }
 
                count = str.Length - i;
 
缺点不能统计数字字数 比如123.456他会当两个单词
 
3、Regex.Matches(txtContent.Text, @"[\W]+").Count
 
Regex.Matches(s, @"[A-Za-z'-.]+").Count 不包括数字的英文单词字数
 
中文字数统计:
 
    public static int ChineseLetterCount(string strText)
 
    {
 
        byte[] byts = System.Text.Encoding.GetEncoding("gb2312").GetBytes(strText);
 
 
 
        return byts.Length - strText.Length;
 
    }
以上都是近似统计
 
 英文字数统计已经很精确了,中文呢字数统计误差有点大,还有待改进
 
不过我在做对比测试的发现word2010统计有一点bug:
 
请看以下测试用例:
 
"I looked at it and said, 'We'll be back in a few days,' " Byron Largent said of the china. (word2010里面字数统计为20)
 
而我写的正则统计为19不知道谁对谁错
 
注意:连词算一个不算两个哦
 
本文作者:网友 来源:网络
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读