How to compare two strings ignoring case in Swift language?

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

    extension String
    {
        func equalIgnoreCase(_ compare:String) -> Bool
        {
            return self.uppercased() == compare.uppercased()
        }
    }
    

    sample of use

    print("lala".equalIgnoreCase("LALA"))
    print("l4la".equalIgnoreCase("LALA"))
    print("laLa".equalIgnoreCase("LALA"))
    print("LALa".equalIgnoreCase("LALA"))
    

提交回复
热议问题