Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if an NSString contains one of the NSStrings in an NSArray?

I'm making an iOS app and I need to figure out if an NSString contains any of the NSStrings in an NSArray.

like image 529
Josh Avatar asked Jan 15 '11 21:01

Josh


3 Answers

BOOL found=NO;
for (NSString *s in arrayOfStrings)
{
  if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) {
    found = YES;
    break;
  }
}
like image 76
Nick Moore Avatar answered Nov 15 '22 15:11

Nick Moore


It may be a silly optimization for your use case, but depending on how large the array is that you are iterating, it may be helpful/more performant to use NSArray's indexOfObjectWithOptions:passingTest: method.

With this method you pass some options and a block that contains your test. Passing the NSEnumerationConcurrent option will allow the evaluation of your block to occur on multiple threads concurrently and potentially speed things up. I reused invariant's test, but in a slightly different way. The block functionally returns a BOOL similar to the "found" variable in invariant's implementation. The "*stop = YES;" line indicates that iterating should stop.

See the NSArray reference documentation for more info. Reference

NSArray *arrayOfStrings = ...;
NSString *stringToSearchWithin = @"...";
NSUInteger index = [arrayOfStrings indexOfObjectWithOptions:NSEnumerationConcurrent 
                                                passingTest:^(id obj, NSUInteger idx, BOOL *stop) 
                    {
                        NSString *s = (NSString *)obj;
                        if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) {
                            *stop = YES;
                            return YES;
                        }
                        return NO;
                    }];
if (arrayOfStrings == nil || index == NSNotFound) 
{
    NSLog(@"The string does not contain any of the strings from the arrayOfStrings");
    return;
}
NSLog(@"The string contains '%@' from the arrayOfStrings", [arrayOfStrings objectAtIndex:index]);
like image 26
Adam Avatar answered Nov 15 '22 15:11

Adam


Very small security improvement on Adam's answer: there is a big issue with "objectAtIndex:" because it is totally not thread-safe and will make your app crash much too often. So I do:

NSArray *arrayOfStrings = ...;
NSString *stringToSearchWithin = ...";
__block NSString *result = nil;
[arrayOfStrings indexOfObjectWithOptions:NSEnumerationConcurrent
                             passingTest:^(NSString *obj, NSUInteger idx, BOOL *stop) 
    {
        if ([stringToSearchWithin rangeOfString:obj].location != NSNotFound)
        {
            result = obj;
            *stop = YES;
            //return YES;
        }
        return NO;
    }];
if (!result) 
    NSLog(@"The string does not contain any of the strings from the arrayOfStrings");
else
    NSLog(@"The string contains '%@' from the arrayOfStrings", result);
like image 27
Cœur Avatar answered Nov 15 '22 14:11

Cœur