I have a large string coming back from an http GET and I'm trying to determine if it has a specific snippet of text or not (please forgive my sins here)
My question is this: Can / Should I use NSRange to determine if this snippet of text does exist?
NSRange textRange;
textRange =[[responseString lowercaseString] rangeOfString:[@"hat" lowercaseString]];
if(textRange.location != NSNotFound)
{
//do something magical with this hat
}
Thank you in advance!
iOS 9.2, Xcode 7.2, ARC enabled
Thanks "mipadi" for the original contribution. I wanted to elaborate and update the answer.
Why would you still use this technique? Well,
- (BOOL)containsString:(NSString *)str
is only supported iOS 8.0 and later.My favorite use of this:
Hope this helps someone! Cheers.
You can check to see if the location is
NSNotFound
:If a string is not found,
rangeOfString:
returns{NSNotFound, 0}
.You could bundle this up into a category on
NSString
if you use it a lot: