iOS判断NSString的length

 1 //方法一:
 2 - (NSUInteger)wordCount:(NSString *)str {  
 3 NSUInteger words = 0;   
 4 NSScanner *scanner = [NSScanner scannerWithString: str]; 
 5  
 6 // Look for spaces, tabs and newlines  
 7 NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];  
 8 while ([scanner scanUpToCharactersFromSet:whiteSpace intoString:nil])
 9 words++;  
10 return words;
11 }
12 
13 //方法二:
14 - (int)countWord:(NSString*) s{
15   int i,n=[s length],l=0,a=0,b=0;
16   unichar c;
17   for(i=0;i<n;i++){
18     c=[s characterAtIndex:i];
19     if(isblank(c)){
20       b++;
21     }else if(isascii(c)){
22       a++;
23     }else{
24       l++;
25     }
26   }
27 //  if(a==0 && l==0) return 0;
28   return l+(int)ceilf((float)(a+b)/2.0);
29 }
posted @ 2012-08-10 17:19  Mr.Songz  阅读(275)  评论(0编辑  收藏  举报