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

20,638

Solution 1

BOOL found=NO;
for (NSString *s in arrayOfStrings)
{
  if ([stringToSearchWithin rangeOfString:s].location != NSNotFound) {
    found = YES;
    break;
  }
}

Solution 2

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]);

Solution 3

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF IN %@", theArray];
BOOL result = [predicate evaluateWithObject:theString];

Solution 4

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);
Share:
20,638
Josh
Author by

Josh

Updated on July 09, 2022

Comments

  • Josh
    Josh almost 2 years

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

  • FreeAppl3
    FreeAppl3 over 12 years
    Thank you Adam! Very useful piece of code, works beautifully!