I\'m building a Twitter iPhone app, and it needs to detect when you enter a hashtag or @-mention within a string in a UITextView.
How do I find all words preceded by
Made a category of NSString for that. It's very simple: Find all words, return all words that start with # to get the hashtags.
Relevant code segment below - rename those methods & the category too...
@implementation NSString (PA)
// all words in a string
-(NSArray *)pa_words {
return [self componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
}
// only the hashtags
-(NSArray *)pa_hashTags {
NSArray *words = [self pa_words];
NSMutableArray *result = [NSMutableArray array];
for(NSString *word in words) {
if ([word hasPrefix:@"#"])
[result addObject:word];
}
return result;
}