NSPredicate与字符串的完全匹配 [英] NSPredicate Exact Match with String
问题描述
我有这样的NSPredicate:
I have a NSPredicate like this:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name CONTAINS %@", myString];
但是会返回包含该字符串的任何内容。例如:
如果我的entity.name的位置:
But that will return anything which contains that string. For example: If my entity.name's where:
text
texttwo
textthree
randomtext
且 myString
text
,那么所有这些字符串将匹配。我想这样,如果 myString
是 text
它将只返回第一个对象的名称 text
并且如果 myString
是 randomtext
,它将返回第四个对象 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
推荐答案
it:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name LIKE[c] %@", myString];
LIKE
匹配字符串?和*作为通配符。 [c]
表示比较不区分大小写。
LIKE
matches strings with ? and * as wildcards. The [c]
indicates that the comparison should be case insensitive.
如果你不想要?和*作为通配符处理,可以使用 ==
而不是 LIKE
:
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];
NSPredicate谓词格式字符串中的更多信息String语法文档。
More info in the NSPredicate Predicate Format String Syntax documentation.
这篇关于NSPredicate与字符串的完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!