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.
You can use
-(NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask;
to get a range for a substring, themask
parameter is used to specify case insensitive match.Example :
As stated in the documentation, the method returns a range like
{NSNotFound, 0}
when the substring isn't found.Important this method raises an exception if the string is
nil
.From iOS 8 you can add the
containsString:
orlocalizedCaseInsensitiveContainsString
method to NSString.Updated Answer for Swift 4
Usage:
Output:
true
Hope it helps. Happy Coding.
The method
should help you.
As similar to the answer provided in the link, but use
options
.See
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask
in Apple doc