【问题标题】:Nspredicate for matching string contains specified characters用于匹配字符串的 Nspredicate 包含指定的字符
【发布时间】:2018-12-07 08:00:45
【问题描述】:

有没有办法像这样使用 nspredicate?

字符串 = whvat

如果字符串包含这些字符 [what]{4},我想搜索它。由于未找到 v,因此上述字符串不会返回 true。如果字符串包含“n”个指定字符,我基本上希望谓词返回 true。字符串中可以有其他字符,但指定的字符肯定存在。字符顺序无关紧要。即使我给 [hwta] 它仍然应该给我 whvat 这个词,因为找到了 4 个指定字符

【问题讨论】:

标签: objective-c nspredicate


【解决方案1】:

这不是很优雅,但您可以尝试为每个字符创建谓词:

NSPredicate *p = [NSPredicate predicateWithFormat:@"self contains 'w' and self contains 'h' and self contains 'a' and self contains 't'"];
NSString *string = @"what";
NSLog(@"%d ", [p evaluateWithObject:string]);

【讨论】:

    猜你喜欢
    • 2011-03-25
    • 1970-01-01
    • 2019-07-25
    • 2019-04-24
    • 1970-01-01
    • 2010-11-22
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多