How to compare two strings ignoring case in Swift language?

后端 未结 16 2618
孤街浪徒
孤街浪徒 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

    You could also make all the letters uppercase (or lowercase) and see if they are the same.

    var a = “Cash”
    var b = “CASh”
    
    if a.uppercaseString == b.uppercaseString{
      //DO SOMETHING
    }
    

    This will make both variables as ”CASH” and thus they are equal.

    You could also make a String extension

    extension String{
      func equalsIgnoreCase(string:String) -> Bool{
        return self.uppercaseString == string.uppercaseString
      }
    }
    
    if "Something ELSE".equalsIgnoreCase("something Else"){
      print("TRUE")
    }
    

提交回复
热议问题