Skip to content

Instantly share code, notes, and snippets.

@NikolaKirev
Last active December 28, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NikolaKirev/7562202 to your computer and use it in GitHub Desktop.
Save NikolaKirev/7562202 to your computer and use it in GitHub Desktop.
Find hashtag words in an NSString using NSPredicate
//A code snippet, that can be used to search for hashtags in a given NSString
//Instantiate a mutable array containing all the words. We sepparate the original string into words.
NSMutableArray *hashtagWordsArray = [NSMutableArray arrayWithArray:[<An NSString> componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]];
NSPredicate *hashtagDetectionPredicate = [NSPredicate predicateWithFormat:@"SELF BEGINSWITH %@", @"#"];
[hashtagWordsArray filterUsingPredicate:hashtagDetectionPredicate];
//Log the result
NSLog(@"Hashtags: %@", hashtagWordsArray);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment