How to compare two strings ignoring case in Swift language? How to compare two strings ignoring case in Swift language? ios ios

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("voila")}

Swift 3+

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


Use caseInsensitiveCompare method:

let a = "Cash"let b = "cash"let c = a.caseInsensitiveCompare(b) == .orderedSameprint(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}