Case insensitive comparison NSString
IosObjective CCocoa TouchNsstringIos Problem Overview
Can anyone point me to any resources about case insensitive comparison in Objective C? It doesn't seem to have an equivalent method to str1.equalsIgnoreCase(str2)
Ios Solutions
Solution 1 - Ios
if( [@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame ) {
// strings are equal except for possibly case
}
The documentation is located at Search and Comparison Methods
Solution 2 - Ios
NSString *stringA;
NSString *stringB;
if (stringA && [stringA caseInsensitiveCompare:stringB] == NSOrderedSame) {
// match
}
Note: stringA &&
is required because when stringA
is nil
:
stringA = nil;
[stringA caseInsensitiveCompare:stringB] // return 0
and so happens NSOrderedSame
is also defined as 0
.
The following example is a typical pitfall:
NSString *rank = [[NSUserDefaults standardUserDefaults] stringForKey:@"Rank"];
if ([rank caseInsensitiveCompare:@"MANAGER"] == NSOrderedSame) {
// what happens if "Rank" is not found in standardUserDefaults
}
Solution 3 - Ios
An alternative if you want more control than just case insensitivity is:
[someString compare:otherString options:NSCaseInsensitiveSearch];
Numeric search and diacritical insensitivity are two handy options.
Solution 4 - Ios
You could always ensure they're in the same case before the comparison:
if ([[stringX uppercaseString] isEqualToString:[stringY uppercaseString]]) {
// They're equal
}
The main benefit being you avoid the potential issue described by matm regarding comparing nil strings. You could either check the string isn't nil before doing one of the compare:options:
methods, or you could be lazy (like me) and ignore the added cost of creating a new string for each comparison (which is minimal if you're only doing one or two comparisons).
Solution 5 - Ios
A new way to do this. iOS 8
let string: NSString = "Café"
let substring: NSString = "É"
string.localizedCaseInsensitiveContainsString(substring) // true
Solution 6 - Ios
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString
Solution 7 - Ios
Try this method
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString
Solution 8 - Ios
Converting Jason Coco's answer to Swift for the profoundly lazy :)
if ("Some String" .caseInsensitiveCompare("some string") == .OrderedSame)
{
// Strings are equal.
}
Solution 9 - Ios
to check with the prefix as in the iPhone ContactApp
([string rangeOfString:prefixString options:NSCaseInsensitiveSearch].location == 0)
this blog was useful for me
Solution 10 - Ios
Alternate solution for swift:
To make both UpperCase:
e.g:
if ("ABcd".uppercased() == "abcD".uppercased()){
}
or to make both LowerCase:
e.g:
if ("ABcd".lowercased() == "abcD".lowercased()){
}
Solution 11 - Ios
On macOS you can simply use -[NSString isCaseInsensitiveLike:]
, which returns BOOL
just like -isEqual:
.
if ([@"Test" isCaseInsensitiveLike: @"test"])
// Success
Solution 12 - Ios
NSMutableArray *arrSearchData;
NSArray *data=[arrNearByData objectAtIndex:i];
NSString *strValue=[NSString stringWithFormat:@"%@", [data valueForKey:@"restName"]];
NSRange r = [strValue rangeOfString:key options:NSCaseInsensitiveSearch];
if(r.location != NSNotFound)
{
[arrSearchData addObject:data];
}