Sina Weibo word count statistics

Source: Internet
Author: User

Sina Weibo word count statistics. A Chinese character is a length, and two English letters, numbers, and punctuation marks are a length. The nsstring length method returns 1 for both Chinese and English numbers and punctuation marks.

When doing digital statistics, the first thing that comes to mind is to use regular expressions (IOS regular expressions ). After checking, the regular expression only returns whether the string contains the text of the specified rule, and does not count the number of Chinese characters in the string.

In fact, you can also think about it later. You can use regular expressions to rule every character in the string, but you don't know the efficiency ~. Anyway, I use the following method to poll the-(void) textviewdidchange :( uitextview *) textview proxy method, which will lead to KaKa input in uitextview, which is quite awkward. This method is not available !!!

-(Nsinteger) counttextviewlength :( nsstring *) textstring {int chinesecharacter = 0; int numandsymbol = 0; For (INT I = 0; I <[textstring length]; I ++) {int TMP = [textstring characteratindex: I]; If (TMP> 0x4e00 & TMP <0x9fff) {nslog (@ "% I is a Chinese character: % @", I, [textstring substringwithrange: nsmakerange (I, 1)]); chinesecharacter ++;} else {numandsymbol ++;} currentlength = chinesecharacter + numandsymbol/2; return currentlength ;}

After Google finds the word count Statistics Code of Sina Weibo shared by keefo, it is used directly. The Code is as follows:

- (int)sinaCountWord:(NSString*)s{    int i,n=[s length],l=0,a=0,b=0;    unichar c;    for(i=0;i<n;i++){        c=[s characterAtIndex:i];        if(isblank(c)){            b++;        }else if(isascii(c)){            a++;        }else{            l++;        }    }    if(a==0 && l==0) return 0;    return l+(int)ceilf((float)(a+b)/2.0);}

Additional learning materials: objc Chinese string # The string learning topic is very good. We strongly recommend that you take a look. Very good. We strongly recommend that you check it out!

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.