Check if strings in an array contain a punctuation or numeric substring?

545 views Asked by At

My method currently checks if a substring exists within an array, but I was wondering if there's a way for it to also work with punctuation and numbers. Say I have an array of:

@[@"apple", @"!!", @"apps!", @"banana"];

and my substring is @"!", I want it to spit out: @"!!", and @"apps!"

Or another example:

@[@"apple", @"6:30", @"6 Pizzas", @"26"];

and my substring is @"6", I want it to spit out: @"6:30", and @"6", and @"6 Pizzas"

Is there a way of doing this using NSRange substringRange = [wordsArray rangeOfString:currentWord];

At the moment punctuation characters aren't being recognized. :/

1

There are 1 answers

0
Bhargavi On

Using NSPredicates makes this work much easier.

NSArray *stringArray = @[@"apple", @"!!", @"apps!", @"banana"];

NSPredicate *predicate = [NSPredicate PredicateWithFormat:@"SELF CONTAINS[cd] '%@'",searchChar];
NSArray *resultArray = [stringArray filteredArrayUsingPredicate:predicate];