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 the "@" or "#" characters within an NSString?
Thanks for your help!
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 the "@" or "#" characters within an NSString?
Thanks for your help!
Here's how you can do it using
NSPredicate
You can try something like this in the UITextView delegate:
where _words, _searchResults and _oldArray are NSArrays.
You can break a string into pieces (words) by using componentsSeparatedByString: and then check the first character of each one.
Or, if you need to do it while the user is typing, you can provide a delegate for the text view and implement textView:shouldChangeTextInRange:replacementText: to see typed characters.
You can use NSRegularExpression class with a pattern like #\w+ (\w stands for word characters).
Use following expression to detect @ or # in string
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...