When I try to filter an array of strings I do not get a match for the value that has a ? in it when using filteredArrayUsingPredicate:. You can substitute the URLs for just a sentence with a ? and you still get the same thing.
Here’s the simplified code:
-(void)test {
NSArray *theURLs = [NSArray arrayWithObjects:@"http://www.google.com", @"http://www.google.com?test=1", nil];
NSString *currentURL = @"http://www.google.com?test=1";
NSLog(@"currentURL %@", currentURL);
NSPredicate *matchURLPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", [currentURL lowercaseString]];
NSLog(@"match predicate %@", matchURLPredicate);
NSArray *filteredArray = [theURLs filteredArrayUsingPredicate:matchURLPredicate];
NSLog(@"filtered array %@", filteredArray);
if ([filteredArray count]== 0) {
NSLog(@"http://www.google.com?test=1 should have been found, but was not");
} else {
NSLog(@"http://www.google.com?test=1 was found");
}
}
If I look for just @”http://www.google.com” it is filtered just fine.
Here’s the corrected code:
-(void)test {
NSArray *theURLs = [NSArray arrayWithObjects:@"http://www.google.com", @"http://www.google.com?test=1", nil];
NSString *currentURL = @"http://www.google.com\\?test=1";
NSLog(@"currentURL %@", currentURL);
NSPredicate *matchURLPredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES[cd] %@", [currentURL lowercaseString]];
NSLog(@"match predicate %@", matchURLPredicate);
NSArray *filteredArray = [theURLs filteredArrayUsingPredicate:matchURLPredicate];
NSLog(@"filtered array %@", filteredArray);
if ([filteredArray count]== 0) {
NSLog(@"http://www.google.com?test=1 should have been found, but was not");
} else {
NSLog(@"http://www.google.com?test=1 was found");
}
}
The
?might be a special character. just escape it: