C # Word Count)

Source: Internet
Author: User

1. RegEx. Matches (S, @ "[A-Za-z0-9] [A-Za-z0-9 '\-.] *"). Count)

RegEx. Matches (S, @ "[A-Za-z0-9] [A-Za-z0-9 \-.] *"). Count (English word conjunction is not a word)

2. String [] STR = filterhtml (content). Split ("~! @ # $ % ^ & * () _ +-= '| \: \ "?> <; ',./". Tochararray ());

Int I = 0;

Foreach (string s in Str)

{

If (S. Length = 0)

I ++;

}

Count = Str. Length-I;

The disadvantage of the 2nd method is that the number of words cannot be counted. For example, 123.456 is used as two words. The above method is only applicable to the number of words in English.

3. RegEx. Matches (txtcontent. Text, @ "[\ W] +"). Count Chinese Characters

RegEx. Matches (strcontent, @ "[0-9] [0-9 '\-.] *"). Count Number of words

Chinese Word Count statistics:

Public static int chineselettercount (string strtext) {byte [] byts = system. Text. encoding. getencoding ("gb2312"). getbytes (strtext );

Return byts. Length-strtext. length;

}

The above are approximate statistics.

The English word count statistics have been very accurate, and the Chinese Word Count statistics have a large error and need to be improved.

However, I found a bug in word2010 statistics during the comparative test:

See the following test cases:

"I looked at it and said, 'we' ll be back in a few days, '" Byron Largent said of the China. (word2010 contains 20 words)

The regular statistics I wrote are 19. I don't know who is right or who is wrong.

Note: The join word is not counted as two. It seems that Microsoft does not count as one word.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.