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