Here’s a quick way to count the number of words in an NSString object. The trick is to use the character set whitespaceAndNewlineCharacterSet which will look for spaces, tabs and newline characters.
- (NSUInteger)wordCount:(NSString *)str { NSUInteger words = 0; NSScanner *scanner = [NSScanner scannerWithString: str]; // Look for spaces, tabs and newlines NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; while ([scanner scanUpToCharactersFromSet:whiteSpace intoString:nil]) words++; return words; } |
If you have another way to reach the same result, please post a code sample.
Maybe a more elegant approach is to use NSLinguisticTagger:
NSString *linguisticTaggerTestString = @”Do any additional setup after loading the view, typically from a nib.”;
NSLinguisticTagger *lingusticTagger = [[NSLinguisticTagger alloc] initWithTagSchemes:[NSArray arrayWithObject:NSLinguisticTagSchemeTokenType] options:0];
[lingusticTagger setString:linguisticTaggerTestString];
[lingusticTagger enumerateTagsInRange:NSMakeRange(0, [linguisticTaggerTestString length])
scheme:NSLinguisticTagSchemeTokenType
options:0 usingBlock:^(NSString *tag, NSRange tokenRange, NSRange sentenceRange, BOOL *stop)
{
NSLog(@”%@ (%@)”, [linguisticTaggerTestString substringWithRange:tokenRange], tag);
}];
You need to use
if ([tag isEqualTo: NSLinguisticTagWord]) { NSLog(…); }
in your block for that to get a sensible result, but then it works.
I usally just use the following
return [[message componentsSeparatedByString:@” “] count];
NSString *str = “some text with new line\nfew spaces and something like non regular punctuation… text me :)”
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”\\w+\\s*” options:NSRegularExpressionCaseInsensitive error:nil];
NSUInteger numberOfMatches = [regex numberOfMatchesInString:str
options:0
range:NSMakeRange(0, [str length])];
NSLog(@”%d words”, numberOfMatches);
How about this :
– (NSUInteger)wordCount:(NSString *)str
{
return [str componentsSeparatedByCharactersInSet:
[NSCharacterSet whitespaceAndNewlineCharacterSet]
count];
}
A bit long for a comment, but I wrote up a comparison of the different approaches here http://ianp.org/2012/04/16/how-many-words-make-a-string/
Cheers,
Ian.
Yes i have :)
you can use – (NSArray *)componentsSeparatedByCharactersInSet:(NSCharacterSet *)separator
– (NSUInteger)anotherWordCount
{
NSString *mystring = @”Think different! Steve Jobs, Apple Inc.”;
NSCharacterSet *whiteSpace = [NSCharacterSet whitespaceAndNewlineCharacterSet];
NSArray *result = [mystring componentsSeparatedByCharactersInSet:whiteSpace];
return result.count;
}
Maybe it’s more fast than a NSSCanner but it uses more memory (for the NSArray creation).
Good Luck :)
You could use a regular expression, like:
NSRegularExpression* regex = [NSRegularExpression regularExpressionWithPattern:@”\\w+” options:0 error:nil];
if (!regex && error) {
NSLog(@”better do more than just call NSLog in production code!”);
}
NSUInteger words = [regex numberOfMatchesInString:str options:0 range:NSMakeRange(0, [str length])];
The regex approach would do better if you wanted to ignore, e.g. stand-alone punctuation, and if you cache the regex (maybe in the thread dictionary) it *may* get better performance.
Why so complicated, everyone?
NSString *wordString = @”long string with lots of words”;
[wordString enumerateSubstringsInRange:NSMakeRange(0, wordString.length) options:NSStringEnumerationByWords usingBlock:^(NSString *substring, NSRange substringRange, NSRange enclosingRange, BOOL *stop) {
NSLog(@”Word:%@”, substring);
//Profit
}];
Nice, I like the use of a block.
NSArray *sampleArray= [str componentsSeparatedByString:@” “];
[sampleArray count];