There are two ways here if you want to filter using CONTAINS[c]
or MATCHES
, here is example with NSPredicate
:
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (self.array as NSArray).filtered(using: searchPredicate)
Now using Contains will filter depending on the string that contains the key word as you said, so three- will be returned because user searched for three:
person searches “three” : search returns “three-”.
The other way is to use MATCHES and here is an example:
let searchPredicate = NSPredicate(format: "SELF MATCHES %@", searchController.searchBar.text!)
let array = (self.array as NSArray).filtered(using: searchPredicate)
Using matches will return the result if its exactly the same as the search key, if i took your example:
person searches “three” : search returns “”.
but
person searches “three-” : search returns “three-”.
Now if you want to ignore the character while displaying you can just trim it if found in cellForRow:
if string.contains("-"){
let trimmedString = string.replacingOccurrences(of: "-", with: "")
}
At the end i think if you used contains with trimming while display, that is the best option for you.