How to count words within a text string?

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

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

4条回答
  •  时光说笑
    2021-01-01 08:16

    One liner accurate solution:

    return [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]].count;
    

    This solution handles consecutive spaces correctly.

提交回复
热议问题