Objective-C:-[NSString wordCount] [英] Objective-C: -[NSString wordCount]
问题描述
以下NSString
类别方法的简单实现是什么,该方法返回self
中的单词数,其中单词由任意数量的连续空格或换行符分隔?另外,字符串将少于140个字符,因此,在这种情况下,我更喜欢简单性&易读性,但会牺牲一些性能.
What's a simple implementation of the following NSString
category method that returns the number of words in self
, where words are separated by any number of consecutive spaces or newline characters? Also, the string will be less than 140 characters, so in this case, I prefer simplicity & readability at the sacrifice of a bit of performance.
@interface NSString (Additions)
- (NSUInteger)wordCount;
@end
我找到了以下解决方案:
I found the following solutions:
- implementation of -[NSString wordCount]
- implementation of -[NSString wordCount] - seems a bit simpler
但是,有没有更简单的方法?
But, isn't there a simpler way?
推荐答案
像我在问题中给出的第二个链接仍然占据统治地位,因为它不仅速度最快,而且事后看来,也是相对简单的
Looks like the second link I gave in my question still reigns as not only the fastest but also, in hindsight, a relatively simple implementation of -[NSString wordCount].
这篇关于Objective-C:-[NSString wordCount]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!