Check if array contains part of a string in Swift?

前端 未结 9 1053
无人共我
无人共我 2020-12-08 15:04

I have an array containing a number of strings. I have used contains() (see below) to check if a certain string exists in the array however I would like to chec

相关标签:
9条回答
  • 2020-12-08 15:13

    Try like this.

    Swift 3.0

    import UIKit
    
    let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
    
    var filterdItemsArray = [String]()
    
    
    func filterContentForSearchText(searchText: String) {
        filterdItemsArray = itemsArray.filter { item in
            return item.lowercased().contains(searchText.lowercased())
        }
    }
    
    filterContentForSearchText(searchText: "Go")
    print(filterdItemsArray)
    

    Output

    ["Google", "Goodbye", "Go"]
    
    0 讨论(0)
  • 2020-12-08 15:14

    If you are just checking if an item exists in a specific array, try this:

    var a = [1,2,3,4,5]
    
    if a.contains(4) {
        print("Yes, it does contain number 4")
    }
    else {
        print("No, it doesn't")
    }
    
    0 讨论(0)
  • 2020-12-08 15:18

    First of all, you have defined an array with a single string. What you probably want is

    let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
    

    Then you can use contains(array, predicate) and rangeOfString() – optionally with .CaseInsensitiveSearch – to check each string in the array if it contains the search string:

    let itemExists = contains(itemsArray) {
        $0.rangeOfString(searchToSearch, options: .CaseInsensitiveSearch) !=  nil
    }
    
    println(itemExists) // true 
    

    Or, if you want an array with the matching items instead of a yes/no result:

    let matchingTerms = filter(itemsArray) {
        $0.rangeOfString(searchToSearch, options: .CaseInsensitiveSearch) !=  nil
    }
    
    println(matchingTerms) // [Google, Goodbye, Go]
    

    Update for Swift 3:

    let itemExists = itemsArray.contains(where: {
        $0.range(of: searchToSearch, options: .caseInsensitive) != nil
    })
    print(itemExists)
    
    let matchingTerms = itemsArray.filter({
        $0.range(of: searchToSearch, options: .caseInsensitive) != nil
    })
    print(matchingTerms)
    
    0 讨论(0)
  • 2020-12-08 15:21

    In Swift 5 with better readability :

    let itemsArray = ["Google", "Goodbye", "Go", "Hello"]
    let searchString = "Googled"
    
    let result = itemsArray.contains(where: searchString.contains)
    print(result) //prints true in the above case.
    
    0 讨论(0)
  • 2020-12-08 15:25
    func filterContentForSearchText(_ searchText: String) {
       filteredString = itemsArray.filter({( item : String) -> Bool in
                return  item.lowercased().contains(searchText.lowercased())
        })
    }
    
    0 讨论(0)
  • 2020-12-08 15:26

    I had the same problem recently, didn't like most of these answers, solved it like this:

    let keywords = ["doctor", "hospital"] //your array
    
    func keywordsContain(text: String) -> Bool { // text: your search text
        return keywords.contains { (key) -> Bool in
            key.lowercased().contains(text.lowercased())
        }
    }
    

    This will also correctly trigger searches like "doc", which many of the above answers do not and is best practice. contains() is more performant than first() != nil source: https://www.avanderlee.com/swift/performance-collections/

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