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?
Why not just do the following?
- (NSUInteger)wordCount {
NSCharacterSet *separators = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSArray *words = [self componentsSeparatedByCharactersInSet:separators];
NSIndexSet *separatorIndexes = [words indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
return [obj isEqualToString:@""];
}];
return [words count] - [separatorIndexes count];
}
I believe you have identified the 'simplest'. Nevertheless, to answer to your original question - "a simple implementation of the following NSString category...", and have it posted directly here for posterity:
@implementation NSString (GSBString)
- (NSUInteger)wordCount
{
__block int words = 0;
[self enumerateSubstringsInRange:NSMakeRange(0,self.length)
options:NSStringEnumerationByWords
usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {words++;}];
return words;
}
@end
There are a number of simpler implementations, but they all have tradeoffs. For example, Cocoa (but not Cocoa Touch) has word-counting baked in:
- (NSUInteger)wordCount {
return [[NSSpellChecker sharedSpellChecker] countWordsInString:self language:nil];
}
It's also trivial to count words as accurately as the scanner simply using [[self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] count]
. But I've found the performance of that method degrades a lot for longer strings.
So it depends on the tradeoffs you want to make. I've found the absolute fastest is just to go straight-up ICU. If you want simplest, using existing code is probably simpler than writing any code at all.
- (NSUInteger) wordCount
{
NSArray *words = [self componentsSeparatedByString:@" "];
return [words count];
}
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].
A Objective-C one-liner version
NSInteger wordCount = word ? ([word stringByTrimmingCharactersInSet:NSCharacterSet.whitespaceAndNewlineCharacterSet.invertedSet].length + 1) : 0;
Swift 3:
let words: [Any] = (string.components(separatedBy: " "))
let count = words.count
来源:https://stackoverflow.com/questions/6171422/objective-c-nsstring-wordcount