Skip to content

Instantly share code, notes, and snippets.

@curthard89
Created August 31, 2011 10:02
Show Gist options
  • Save curthard89/1183218 to your computer and use it in GitHub Desktop.
Save curthard89/1183218 to your computer and use it in GitHub Desktop.
Word Count for NSString
- (NSInteger)wordCount
{
NSInteger length = [self length];
NSRange inputRange = NSMakeRange( 0, length );
NSInteger count = 0;
while( YES )
{
NSRange range = [self rangeOfCharacterFromSet:[NSCharacterSet whitespaceCharacterSet]
options:NSCaseInsensitiveSearch
range:inputRange];
if( range.location == NSNotFound )
{
break;
} else {
count++;
NSInteger pos = ( range.location + range.length );
inputRange = NSMakeRange( pos, length - pos );
}
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment