Swift 5 determines whether the array contains the string, ignoring case

System: Mac OS 10.15.2, XCode 11.3, swift 5.0
Writing Time: 2020-01-08

Explanation

Swift 5 determines whether the array contains the string, ignoring case

let list = ["kAshif", "1", "2"]
let list2 = ["3", "4"]
let word = "Kashif"

if list.contains(where: { $0.caseInsensitiveCompare(word) == .orderedSame }) {
    print("list contains kAshif is true")
}

if list2.contains(where: { $0.caseInsensitiveCompare(word) == .orderedSame}) {
    print("list2 contains kAshif is true")
}

// print > list contains kAshif is true

reference

https://stackoverflow.com/questions/31329568/check-if-a-string-exists-in-an-array-case-insensitively/59644230#59644230

Published 127 original articles · won praise 12 · views 20000 +

Guess you like

Origin blog.csdn.net/zgpeace/article/details/103896507