C # implement the word count function of the same algorithm as Sina Weibo and take it away.

Source: Internet
Author: User

Remaining word count on Weibo official SinaAlgorithm.

The text parameter is the body content.

Isoutofrange is used to determine whether the text overflows to control the available status of the send button.

 1   Public   Static   String Getcontentlengthstring ( String Text, Out   Bool  Isoutofrange)  2   {  3 TEXT = Text. Trim ();  4 TEXT = RegEx. Replace (text, "  \ R \ n  " , "  \ N  "  );  5       Int Textlength = 0  ;  6       If (Text. length> 0  )  7  {  8           Int Min = 41 , Max = 140 , Urllen = 20  ;  9           VaR N = Text;  10           VaR R = RegEx. Matches (text, @"  Http: // [a-zA-Z0-9] + (\. [a-zA-Z0-9] +) + ([-A-Z0-9a-z _ $. +! * ()/\\\,:;@&= ?~ # %] *) * "  );  11           VaR Total = 0  ;  12           For ( Int M = 0 , P = R. Count; m <p; m ++ )  13   {  14               VaR Url = R [M]. value; 15               VaR Bytelen = URL. Length + RegEx. Matches (URL, @"  [^ \ X00-\ X80]  "  ). Count;  16               If (RegEx. ismatch (URL, @"  ^ Http://t.cn)  "  ))  17   {  18                   Continue ;  19   }  20               Else   If (RegEx. ismatch (URL, @"  ^ (Http: \/) + (weibo.com | weibo.cn)  "  ))  21   {  22 Total + = bytelen <= min? Bytelen: (bytelen <= max? Urllen: (bytelen-MAX + Urllen )); 23   }  24               Else  25   {  26 Total + = bytelen <= max? Urllen: (bytelen-MAX + Urllen );  27   }  28 N = n. Replace (URL, ""  );  29   } 30 Textlength = ( Int ) Math. Ceiling (total + N. Length + RegEx. Matches (n, @"  [^ \ X00-\ X80]  " ). Count )/ 2.00d  );  31   }  32   33       Int Textremainlength = 140 - Textlength; 34       String Template = String  . Empty;  35       If (Textremainlength> = 0  )  36   {  37 Template = "  You can also enter {0: N0} characters.  "  ;  38 Isoutofrange = False  ;  39   }  40       Else  41   {  42 Template = "  It has exceeded {0: N0} characters  "  ;  43   44 Isoutofrange = True ;  45   }  46       Return   String  . Format (template, math. Abs (textremainlength ));  47 }

Execution result

Related Article

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.