NSPredicate 与字符串完全匹配 [英] NSPredicate Exact Match with String

查看:33
本文介绍了NSPredicate 与字符串完全匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样的 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

并且 myStringtext 那么所有这些字符串都会匹配.我希望这样,如果 myStringtext 它只会返回名称为 text 的第一个对象,如果 myStringrandomtext 它将返回名为 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 与字符串完全匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆