Find one string in another with case insensitive in Objective-C
As similar to the answer provided in the link, but use options
.
See - (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask
in Apple doc
NSString *string = @"hello bla bla";
if ([string rangeOfString:@"BLA" options:NSCaseInsensitiveSearch].location == NSNotFound)
{
NSLog(@"string does not contain bla");
}
else
{
NSLog(@"string contains bla!");
}
NSString *string = @"hello BLA";
if ([string rangeOfString:@"bla" options:NSCaseInsensitiveSearch].location == NSNotFound) {
NSLog(@"string does not contain bla");
} else {
NSLog(@"string contains bla!");
}
From iOS 8 you can add the containsString:
or localizedCaseInsensitiveContainsString
method to NSString.
if ([string localizedCaseInsensitiveContainsString:@"BlA"]) {
NSLog(@"string contains Case Insensitive bla!");
} else {
NSLog(@"string does not contain bla");
}