如何快速忽略大小写比较两个字符串?例如:
var a = "Cash" var b = "cash"
如果我们比较var a和var b是否有任何方法可以返回true
试试这个:
var a = "Cash" var b = "cash" let result: NSComparisonResult = a.compare(b, options: NSStringCompareOptions.CaseInsensitiveSearch, range: nil, locale: nil) // You can also ignore last two parameters(thanks 0x7fffffff) //let result: NSComparisonResult = a.compare(b, options: NSStringCompareOptions.CaseInsensitiveSearch)
结果是NSComparisonResult枚举的类型:
enum NSComparisonResult : Int { case OrderedAscending case OrderedSame case OrderedDescending }
因此,您可以使用if语句:
if result == .OrderedSame { println("equal") } else { println("not equal") }