Check if a string exists in an array case insensitively

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

Declaration:

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

then this

contains(listArray, word) 

Ret

相关标签:
11条回答
  • 2020-12-09 07:54

    Expanding on @Govind Kumawat's answer

    The simple comparison for a searchString in a word is:

    word.range(of: searchString, options: .caseInsensitive) != nil
    

    As functions:

    func containsCaseInsensitive(searchString: String, in string: String) -> Bool {
        return string.range(of: searchString, options: .caseInsensitive) != nil
    }
    
    func containsCaseInsensitive(searchString: String, in array: [String]) -> Bool {
        return array.contains {$0.range(of: searchString, options: .caseInsensitive) != nil}
    }
    
    func caseInsensitiveMatches(searchString: String, in array: [String]) -> [String] {
        return array.compactMap { string in
            return string.range(of: searchString, options: .caseInsensitive) != nil
                ? string
                : nil
        }
    }
    
    0 讨论(0)
  • 2020-12-09 08:02

    Xcode 8 • Swift 3 or later

    let list = ["kashif"]
    let word = "Kashif"
    
    if list.contains(where: {$0.caseInsensitiveCompare(word) == .orderedSame}) {
        print(true)  // true
    }
    

    alternatively:

    if list.contains(where: {$0.compare(word, options: .caseInsensitive) == .orderedSame}) {
        print(true)  // true
    }
    

    if you would like to know the position(s) of the element in the array (it might find more than one element that matches the predicate):

    let indices = list.indices.filter { list[$0].caseInsensitiveCompare(word) == .orderedSame }
    print(indices)  // [0]
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-09 08:04

    For checking if a string exists in a array (case insensitively), please use

    listArray.localizedCaseInsensitiveContainsString(word) 
    

    where listArray is the name of array and word is your searched text

    This code works in Swift 2.2

    0 讨论(0)
  • 2020-12-09 08:08

    For checking if a string exists in a array with more Options(caseInsensitive, anchored/search is limited to start)

    using Foundation range(of:options:)

    let list = ["kashif"]
    let word = "Kashif"
    
    
    if list.contains(where: {$0.range(of: word, options: [.caseInsensitive, .anchored]) != nil}) {
        print(true)  // true
    }
    
    if let index = list.index(where: {$0.range(of: word, options: [.caseInsensitive, .anchored]) != nil}) {
        print("Found at index \(index)")  // true
    }
    
    0 讨论(0)
  • 2020-12-09 08:10

    SWIFT 3.0:

    Finding a case insensitive string in a string array is cool and all, but if you don't have an index it can not be cool for certain situations.

    Here is my solution:

    let stringArray = ["FOO", "bar"]()
    if let index = stringArray.index(where: {$0.caseInsensitiveCompare("foo") == .orderedSame}) {
       print("STRING \(stringArray[index]) FOUND AT INDEX \(index)")
       //prints "STRING FOO FOUND AT INDEX 0"                                             
    }
    

    This is better than the other answers b/c you have index of the object in the array, so you can grab the object and do whatever you please :)

    0 讨论(0)
提交回复
热议问题