How to count words within a text string?

前端 未结 4 991
面向向阳花
面向向阳花 2021-01-01 07:47

On iOS, how can I count words within a specific text string?

4条回答
  •  醉梦人生
    2021-01-01 07:59

    I think this method is better:

    __block int wordCount = 0;
    NSRange range = {0,self.text.length };
    [self.text enumerateSubstringsInRange:range options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
        wordCount++;
    }];
    

    As a reference check the video of the session 215 of the WWDC 2012: Text and Linguistic Analysis by Douglas Davidson

提交回复
热议问题