ios 不区分大小写的比较 NSString
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2582306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Case insensitive comparison NSString
提问by Tejaswi Yerukalapudi
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)
任何人都可以向我指出有关 Objective C 中不区分大小写比较的任何资源吗?它似乎没有等效的方法str1.equalsIgnoreCase(str2)
回答by Jason Coco
if( [@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame ) {
// strings are equal except for possibly case
}
The documentation is located at Search and Comparison Methods
该文档位于搜索和比较方法
回答by ohho
NSString *stringA;
NSString *stringB;
if (stringA && [stringA caseInsensitiveCompare:stringB] == NSOrderedSame) {
// match
}
Note:stringA &&
is required because when stringA
is nil
:
注意:stringA &&
是必需的,因为 whenstringA
是nil
:
stringA = nil;
[stringA caseInsensitiveCompare:stringB] // return 0
and so happens NSOrderedSame
is also defined as 0
.
等恰好NSOrderedSame
也定义为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
}
回答by drawnonward
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.
数字搜索和变音不敏感是两个方便的选项。
回答by Chris Wooden
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).
主要好处是您可以避免 matm 描述的有关比较 nil 字符串的潜在问题。您可以在执行其中一种compare:options:
方法之前检查字符串是否为空,或者您可能很懒惰(像我一样)并忽略为每次比较创建新字符串的额外成本(如果您只做一个,这是最小的或两个比较)。
回答by Govind
A new way to do this. iOS 8
一种新的方法来做到这一点。iOS 8
let string: NSString = "Café"
let substring: NSString = "é"
string.localizedCaseInsensitiveContainsString(substring) // true
回答by WhirlWind
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString
回答by Reena
Try this method
试试这个方法
- (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString
回答by Tasik
Converting Jason Coco's answer to Swiftfor the profoundly lazy :)
将 Jason Coco 的答案转换为Swift以应对极度懒惰的人:)
if ("Some String" .caseInsensitiveCompare("some string") == .OrderedSame)
{
// Strings are equal.
}
回答by iMeMyself
回答by FARAZ
Alternate solution for swift:
swift的替代解决方案:
To make both UpperCase:
要使两个大写:
e.g:
例如:
if ("ABcd".uppercased() == "abcD".uppercased()){
}
or to make both LowerCase:
或使两个小写:
e.g:
例如:
if ("ABcd".lowercased() == "abcD".lowercased()){
}