Ivan Titkov
Ivan Titkov

Reputation: 389

Search substring with wildcard symbol using NSPredicate

I try to find string which contains substring with wildcard symbol, for example string should contains '*' and 'p'. At the same time string could start,finish and contains between symbols arbitrary amount of characters. This my is example

let dataSource = [
    "Domain CheckService",
    "IMEI check",
    "Compliant about service provider",
    "Compliant about TRA",
    "Enquires",
    "Suggestion",
    "SMS* Spam",
    "Poor Coverage",
    "Pure *Signal",
    "Help Salim"
]

let searchString = "***p*"
let predicate = NSPredicate(format: "SELF LIKE %@", searchString)
let searchDataSource = dataSource.filter { predicate.evaluate(with: $0) }

I use such mask to find string that satisfy conditions:

any symbols + '*' + any symbols + 'p' + any symbols

but this variant doesn't work according * is reserved symbol in LAKE, so as result i get wrong search set. So this is a problem? How search substring with '*' symbol in it.

Upvotes: 0

Views: 874

Answers (1)

Ivan Titkov
Ivan Titkov

Reputation: 389

The variant that lead to right result: use wildcard the same way as escape symbols in regular expression, add double backslash before wildcard correct mask looks like:

 let searchString = "*\\**p*"

Upvotes: 1

Related Questions