Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
NSString Category for search string contents
//NOTE: containsString is implemented in iOS 8 and later by Apple
// Please use this version if trying to support iOS 7 and below
@interface NSString (StringContents)
- (BOOL)containsString:(NSString*)substring;
- (BOOL)containsString:(NSString*)substring ignoreCase:(BOOL)ignoreCase;
- (BOOL)containsFormat:(NSString*)regex;
@end
@implementation NSString (StringContents)
- (BOOL)containsString:(NSString*)substring
{
return (substring.length && [self rangeOfString:substring].length);
}
- (BOOL)containsString:(NSString*)substring ignoreCase:(BOOL)ignoreCase
{
if(ignoreCase)
return (substring.length && [self rangeOfString:substring options:NSCaseInsensitiveSearch].length);
else
return (substring.length && [self rangeOfString:substring].length);
}
- (BOOL)containsFormat:(NSString*)regex
{
NSPredicate *regextest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
return [regextest evaluateWithObject:self];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.