How to compare two strings ignoring case in Swift language?

后端 未结 16 2582
孤街浪徒
孤街浪徒 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 05:52

    CORRECT WAY:

    let a: String = "Cash"
    let b: String = "cash"
    
    if a.caseInsensitiveCompare(b) == .orderedSame {
        //Strings match 
    }
    

    Please note: ComparisonResult.orderedSame can also be written as .orderedSame in shorthand.

    OTHER WAYS:

    a.

    if a.lowercased() == b.lowercased() {
        //Strings match 
    }
    

    b.

    if a.uppercased() == b.uppercased() {
        //Strings match 
    }
    

    c.

    if a.capitalized() == b.capitalized() {
        //Strings match 
    }
    

提交回复
热议问题