Check if a string exists in an array case insensitively

前端 未结 11 1557
情歌与酒
情歌与酒 2020-12-09 07:45

Declaration:

let listArray = [\"kashif\"]
let word = \"kashif\"

then this

contains(listArray, word) 

Ret

11条回答
  •  渐次进展
    2020-12-09 08:02

    You can add an extension:

    Swift 5

    extension Array where Element == String {
        func containsIgnoringCase(_ element: Element) -> Bool {
            contains { $0.caseInsensitiveCompare(element) == .orderedSame }
        }
    }
    

    and use it like this:

    ["tEst"].containsIgnoringCase("TeSt") // true
    

提交回复
热议问题