Cor*_*ode 24 core-data objective-c nsstring nspredicate ios
我有这样一个NSPredicate:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name CONTAINS %@", myString];
Run Code Online (Sandbox Code Playgroud)
但是这将返回包含该字符串的任何内容.例如:如果我的entity.name的位置:
text
texttwo
textthree
randomtext
Run Code Online (Sandbox Code Playgroud)
并且myString
是text
那么所有这些字符串会匹配.我想它,这样,如果myString
是text
那只名为返回的第一个对象text
,如果myString
是randomtext
,将与名称返回第四对象randomtext
.我也在寻找它不区分大小写,它忽略了空格
And*_*sen 61
这应该这样做:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name LIKE[c] %@", myString];
Run Code Online (Sandbox Code Playgroud)
LIKE
匹配字符串?和*作为通配符.本[c]
表示比较应不区分大小写.
如果你不想要?和*被视为通配符,您可以使用==
而不是LIKE
:
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"entity.name ==[c] %@", myString];
Run Code Online (Sandbox Code Playgroud)
NSPredicate谓词格式字符串语法文档中的更多信息.
das*_*ght 13
您可以将正则表达式匹配器与谓词一起使用,如下所示:
NSString *str = @"test";
NSMutableString *arg = [NSMutableString string];
[arg appendString:@"\\s*\\b"];
[arg appendString:str];
[arg appendString:@"\\b\\s*"];
NSPredicate *p = [NSPredicate predicateWithFormat:@"SELF matches[c] %@", arg];
NSArray *a = [NSArray arrayWithObjects:@" test ", @"test", @"Test", @"TEST", nil];
NSArray *b = [a filteredArrayUsingPredicate:p];
Run Code Online (Sandbox Code Playgroud)
上面的代码构造了一个正则表达式,该表达式在开头和/或结尾处匹配带有可选空格的字符串,目标字由"单词边界"标记围绕\b
.在[c]
之后matches
的意思是"匹配不区分大小写".
此示例使用字符串数组; 使其在您的环境中工作,替换SELF
为entity.name
.
归档时间: |
|
查看次数: |
49243 次 |
最近记录: |