Created
August 31, 2011 10:02
-
-
Save curthard89/1183218 to your computer and use it in GitHub Desktop.
Word Count for NSString
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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