问题描述
我有一个这样的 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
推荐答案
应该这样做:
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 谓词格式字符串语法中的更多信息 文档.
More info in the NSPredicate Predicate Format String Syntax documentation.
这篇关于NSPredicate 与字符串完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!