【发布时间】:2014-10-30 07:19:24
【问题描述】:
我有一个类似的数组
NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"How many degrees ", @"Which club degree", @"body building", nil];
只想过滤那些包含度数的字符串。我用的是老IOS,没有[string stringcontains]方法
所需的数组必须有 -> 1. 多少度 2. 哪个俱乐部度
我使用的第一种方法
NSString *strToMatch=@"degree";
for (NSString *description in arr)
{
NSComparisonResult result = [description compare:strToMatch options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [strToMatch length])];
if (result == NSOrderedSame)
{
// statement to run on comparison succeeded..
}
}
第二种方法
for (NSString *description in arr)
{
if ([description rangeOfString:strToMatch options:NSCaseInsensitiveSearch].location != NSNotFound)
{
NSLog(@"I am matched....");
}
}
它工作正常。我已经向您展示了比较单个字符串的代码,但我必须将此数组与另一个字符串数组进行比较。我想加快我的代码。有没有其他更好的方法可以找到它。
我们如何创建一个 NSPredicate 来比较两个字符串数组。在第二个数组中查找第一个字符串数组作为子字符串。
哪种方法快。
提前致谢。
【问题讨论】:
标签: ios objective-c nsmutablearray string-comparison