【问题标题】:Using predicates find strings begining and ending with A使用谓词查找以 A 开头和结尾的字符串
【发布时间】:2013-03-13 14:02:48
【问题描述】:

我能够从包含以A 开头和结尾的字符串的数组中找到所有值。

我可以使用NSString 类中的-hasPrefix-hasSuffix

但我没有找到任何方法来使用 NSPredicate 类找到相同的方法。

请帮帮我。提前致谢。

【问题讨论】:

  • 看我的回答对你有帮助。

标签: ios objective-c cocoa nsarray nspredicate


【解决方案1】:

试试这个可能对你有帮助,

NSPredicate *p = [NSPredicate predicateWithFormat:@"SELF like [c]'A*A'"];
    NSArray *a = [NSArray arrayWithObjects:@" AhfjA ", @"test1", @"Test", @"AntA", nil];
    NSArray *b = [a filteredArrayUsingPredicate:p];
    NSLog(@"%@",b);

【讨论】:

  • 为什么这不起作用,请检查 NSArray names=@[@"John",@"Anoop", @"Superman",@"Gilly",@"Adams",@ “迈克”,@“福丽娜”,@“小威”,@“aDolf”]; NSPredicate *predicate=[NSPredicate predicateWithFormat:@"SELF like [c]'AA'"]; NSArray *ends=[names filteredArrayUsingPredicate:predicate]; NSLog(@"->%@",ends);
  • 我验证了这个工作正常。 NSArray *a = [NSArray arrayWithObjects:@"John",@"Anoopa",@"Supermana",@"Gilly",@"Adams",@"Mike",@"Forina",@"Serena",@" aDolf",nil];
  • @AnoopVaidya:此谓词查找所有以 A 开头以 A 结尾的名称,如问题中所述。您的代码会查找以 A 开头的名称 以 A 结尾的名称。您的代码还会返回以 A 开头和结尾的名称的重复结果。
  • XCOde 糟透了...需要清理 :(
  • @MartinR:是的,我同意 Sunny 的回答比我好 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多