C # Word count (Letters, numbers, kanji, symbols)

Source: Internet
Author: User

namespaceTest 1{ Public Partial classForm1:form { PublicForm1 () {InitializeComponent ();  This. richTextBox1.Text ="g How are you \rs\rs? d dsddddddddddssssss, ' \ \ Kit Kat '; l; "" Sssssssssssssssssssssssssssssssssssssssssssssssssssssssssasdddddddddddddddddddd\ Tddddddddddddddddddddddddddddddddddddddddddddddddda"; }        BOOLSkipspace =true; //window Loading        Private voidForm1_Load (Objectsender, EventArgs e)            {Richtextbox1.focus (); intcnt1=0, cnt2=0, cnt3=0, cnt4=0; if( This. richtextbox1.text! =NULL)            {                stringsline; strings =richTextBox1.Text; if(skipspace) {{sline= S.replace ("?",""). Replace (" ",""). Replace ("\ t",""). Replace ("\ n",""). Replace ("\ r",""); Cnt2+=getbytelength (sline); Cnt1+=getwordlength (sline); Cnt3+=getdigitallength (sline); Cnt4+=getcharlength (sline); Textbox5.text= Cnt1. ToString ();//Word CountTextbox6.text = Cnt2. ToString ();//number of bytes (excluding "", "\ T", "\ n", "\ r", "?") )Textbox7.text = Cnt3. ToString ();//Number of NumbersTextbox8.text = Cnt4. ToString ();//Number of lettersTextbox9.text = (cnt2-cnt1). ToString ();//number of Chinese charactersTextbox10.text = (Cnt2-cnt3-cnt4-(CNT2-CNT1) *2). ToString ();//Number of symbols                    }                }            }        }        //richtextbox1.text Content Change events        Private voidRichtextbox1_textchanged (Objectsender, EventArgs e) {            BOOLSkipspace =true; intcnt1=0, cnt2=0, cnt3=0, cnt4=0; if( This. richtextbox1.text! =NULL)            {                stringsline; strings =richTextBox1.Text; if(skipspace) {sline= S.replace ("?",""). Replace (" ",""). Replace ("\ t",""). Replace ("\ n",""). Replace ("\ r",""); Cnt2+=getbytelength (sline); Cnt1+=getwordlength (sline); Cnt3+=getdigitallength (sline); Cnt4+=getcharlength (sline); Textbox5.text= Cnt1. ToString ();//Word CountTextbox6.text = Cnt2. ToString ();//number of bytes (excluding "", "\ T", "\ n", "\ r", "?") )Textbox7.text = Cnt3. ToString ();//Number of NumbersTextbox8.text = Cnt4. ToString ();//Number of lettersTextbox9.text = (cnt2-cnt1). ToString ();//number of Chinese charactersTextbox10.text = (Cnt2-cnt3-cnt4-(CNT2-CNT1) *2). ToString ();//Number of symbols                }            }        }         /// <summary>        ///return word Count/// </summary>        /// <param name= "s" ></param>        /// <returns></returns>        Private intGetwordlength (strings) {if(s! =NULL)                returns.length; Else                return 0; }        /// <summary>        ///return Number (0~9) number of words/// </summary>        /// <param name= "s" ></param>        /// <returns></returns>        Private intGetdigitallength (strings) {intLX =0; Char[] Q =S.tochararray ();  for(inti =0; i < q.length; i++)            {                if((int) Q[i] >= -&& (int) Q[i] <= $) {lx+=1; }            }            returnLX; }        /// <summary>        ///return Letter (a~z-a~z) number of words/// </summary>        /// <param name= "s" ></param>        /// <returns></returns>        Private intGetcharlength (strings) {intLZ =0; Char[] Q = S.tolower (). ToCharArray ();//uppercase letters converted to lowercase             for(inti =0; i < q.length; i++)            {                if((int) Q[i] >= the&& (int) Q[i] <=122)//Lowercase Letters{LZ+=1; }            }            returnLZ; }        /// <summary>        ///returns the number of bytes/// </summary>        /// <param name= "s" ></param>        /// <returns></returns>        Private intGetbytelength (strings) {intLH =0; Char[] Q =S.tochararray ();  for(inti =0; i < q.length; i++)            {                if((int) Q[i] >=0x4e00&& (int) Q[i] <=0x9fa5)//Kanji{LH+=2; }                Else{LH+=1; }            }            returnLH; }    }}

Excerpted from http://blog.csdn.net/ou832339/article/details/20343439

C # Word count (Letters, numbers, kanji, symbols)

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.