How to compare two strings ignoring case in Swift language?

后端 未结 16 2623
孤街浪徒
孤街浪徒 2020-12-13 05:28

How can we compare two strings in swift ignoring case ? for eg :

var a = \"Cash\"
var b = \"cash\"

Is there any method that will return tru

16条回答
  •  猫巷女王i
    2020-12-13 05:55

    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

提交回复
热议问题