How to compare two strings ignoring case in Swift language?

后端 未结 16 2627
孤街浪徒
孤街浪徒 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条回答
  •  长情又很酷
    2020-12-13 06:06

    localizedCaseInsensitiveContains : Returns whether the receiver contains a given string by performing a case-insensitive, locale-aware search

    if a.localizedCaseInsensitiveContains(b) {
        //returns true if a contains b (case insensitive)
    }
    

    Edited:

    caseInsensitiveCompare : Returns the result of invoking compare(_:options:) with NSCaseInsensitiveSearch as the only option.

    if a.caseInsensitiveCompare(b) == .orderedSame {
        //returns true if a equals b (case insensitive)
    }
    

提交回复
热议问题