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!