How to compare two strings ignoring case in Swift language?

Try this :

For older swift:

var a : String = "Cash"
var b : String = "cash"

if(a.caseInsensitiveCompare(b) == NSComparisonResult.OrderedSame){
    println("Et voila")
}

Swift 3+

var a : String = "Cash"
var b : String = "cash"
    
if(a.caseInsensitiveCompare(b) == .orderedSame){
    print("Et voila")
}

Use caseInsensitiveCompare method:

let a = "Cash"
let b = "cash"
let c = a.caseInsensitiveCompare(b) == .orderedSame
print(c) // "true"

ComparisonResult tells you which word comes earlier than the other in lexicographic order (i.e. which one comes closer to the front of a dictionary). .orderedSame means the strings would end up in the same spot in the dictionary


if a.lowercaseString == b.lowercaseString {
    //Strings match
}

Try this:

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)

result is type of NSComparisonResult enum:

enum NSComparisonResult : Int {

    case OrderedAscending
    case OrderedSame
    case OrderedDescending
}

So you can use if statement:

if result == .OrderedSame {
    println("equal")
} else {
    println("not equal")
}