Filter array of strings, including "like" condition Filter array of strings, including "like" condition ios ios

Filter array of strings, including "like" condition


Use contains instead:

let arr = ["Hello","Bye","Halo"]let filtered = arr.filter { $0.contains("lo") }print(filtered)

Output

["Hello", "Halo"]

Thanks to @user3441734 for pointing out that functionality is of course only available when you import Foundation


In Swift 3.0

let terms = ["Hello","Bye","Halo"]var filterdTerms = [String]()func filterContentForSearchText(searchText: String) {    filterdTerms = terms.filter { term in        return term.lowercased().contains(searchText.lowercased())    }}filterContentForSearchText(searchText: "Lo")print(filterdTerms)

Output

["Hello", "Halo"]


Swift 3.1

let catalogNames = [ "Hats", "Coats", "Trousers" ]let searchCatalogName = "Hats"let filteredCatalogNames = catalogNames.filter { catalogName in     return catalogName.localizedCaseInsensitiveContains(searchCatalogName)}print(filteredCatalogNames)


matomo