谁能告诉我Objective C中不区分大小写比较的资料?它似乎没有与str1.equalsIgnoreCase(str2)等效的方法


当前回答

if( [@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame ) {
  // strings are equal except for possibly case
}

文档位于搜索和比较方法

其他回答

- (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString
if( [@"Some String" caseInsensitiveCompare:@"some string"] == NSOrderedSame ) {
  // strings are equal except for possibly case
}

文档位于搜索和比较方法

把Jason Coco的答案转换成Swift的懒人版:)

if ("Some String" .caseInsensitiveCompare("some string") == .OrderedSame)
{
  // Strings are equal.
}

试试这个方法

- (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString

一种新的方法。iOS 8

let string: NSString = "Café"
let substring: NSString = "É"

string.localizedCaseInsensitiveContainsString(substring) // true