iPhone: find one string in another with case insensetive

My question is similar to How do I check if a string contains another string in Objective-C?

How can I check if a string (NSString) contains another smaller string but with ignoring case?

NSString *string = @"hello bla bla";

I was hoping for something like:

NSLog(@"%d",[string containsSubstring:@"BLA"]);

Anyway is there any way to find if a string contains another string with ignore case ? But please do not convert both strings to UpperCase or to LowerCase.


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!");
}

从iOS 8开始,您可以将containsString:localizedCaseInsensitiveContainsString方法添加到NSString。

if ([string localizedCaseInsensitiveContainsString:@"BlA"]) {
    NSLog(@"string contains Case Insensitive bla!");
} else {
    NSLog(@"string does not contain bla");
}

NSString *string = @"hello BLA";
if ([string rangeOfString:@"bla" options:NSCaseInsensitiveSearch].location == NSNotFound) {
    NSLog(@"string does not contain bla");
} else {
    NSLog(@"string contains bla!");
}
链接地址: http://www.djcxy.com/p/73994.html

上一篇: 搜索NSString是否包含值

下一篇: iPhone:在另一个情况下,发现一个字符串不起作用