Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSPredicate Exact Match with String

Tags:

I have a NSPredicate like this:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name CONTAINS %@", myString]; 

But that will return anything which contains that string. For example: If my entity.name's where:

text texttwo textthree randomtext 

and the myString was text then all of those strings would match. I would like it so that if myString is text it would only return the first object with the name text and if myString was randomtext it would return the fourth object with the name randomtext. I am also looking for it to be case insensitive and that it ignores whitespace

like image 959
CoreCode Avatar asked Jul 22 '12 03:07

CoreCode


1 Answers

This should do it:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name LIKE[c] %@", myString]; 

LIKE matches strings with ? and * as wildcards. The [c] indicates that the comparison should be case insensitive.

If you don't want ? and * to be treated as wildcards, you can use == instead of LIKE:

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name ==[c] %@", myString]; 

More info in the NSPredicate Predicate Format String Syntax documentation.

like image 170
Andrew Madsen Avatar answered Sep 22 '22 08:09

Andrew Madsen