Check if array contains part of a string in Swift? -
i have array containing number of strings. have used contains()
(see below) check if string exists in array check if part of string in array?
itemsarray = ["google, goodbye, go, hello"] searchtosearch = "go" if contains(itemsarray, stringtosearch) { nslog("term exists") } else { nslog("can't find term") }
the above code checks if value present within array in entirety find "google, google , go"
try this.
let itemsarray = ["google", "goodbye", "go", "hello"] let searchtosearch = "go" let filteredstrings = itemsarray.filter({(item: string) -> bool in var stringmatch = item.lowercasestring.rangeofstring(searchtosearch.lowercasestring) return stringmatch != nil ? true : false })
filteredstrings
contain list of strings having matched sub strings.
in swift array
struct provides filter method, filter provided array based on filtering text criteria.
Comments
Post a Comment