views:

54

answers:

1

I would like to create a predicate to search for a specific letter at the start of each word in a string of words e.g. all words starting with A in @"The man ate apples", would return ate and apples. Is it possible to create such a predicate? Thank you.

+1  A: 

An NSPredicate returns true or false when evaluated, so you cannot create a predicate that returns all the words starting with "a".

You can create a predicate that returns true for words starting with "a", then split the sentence to world, and filter the good words with predicate something like:

NSArray words = [sentence componentsJoinedByString:@" "];
NSPredicate *beginsWithA = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'a'"];
NSArray *wordsBeginsWithA = [words filteredArrayUsingPredicate:beginsWithA];
mfazekas