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

Tags:

String

Ios

Swift