Objective-C: -[NSString wordCount]

后端 未结 7 1202
孤街浪徒
孤街浪徒 2020-12-05 21:39

What\'s a simple implementation of the following NSString category method that returns the number of words in self, where words are separated by an

7条回答
  •  臣服心动
    2020-12-05 21:54

    A Objective-C one-liner version

    NSInteger wordCount = word ? ([word stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet.invertedSet].length + 1) : 0;
    

提交回复
热议问题