Check if array contains part of a string in Swift?

前端 未结 9 1100
无人共我
无人共我 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"]
    

提交回复
热议问题