How to find beginswith word in a string using NSPredicate?

Viewed 29270

I am searching for a solution on how to format NSPredicate to search correct word in a string of text. Currently I am using this code:

NSPredicate *predicate = [NSPredicate predicateWithFormat:
                          @"(content CONTAINS[c] %@)", word];

but it returns wrong resuls if the word is short, for example: if the word is 'ail' it will return all strings with words which include this 3 letters.. But I don't need such abstract search, so how to search words 'beginswith' my word in a string?

5 Answers

Try using compound:

NSCompoundPredicate *bPredicate =  [NSCompoundPredicate orPredicateWithSubpredicates:@[[NSPredicate predicateWithFormat:@"content BEGINSWITH[cd] %@",_SearchBar.text], [NSPredicate predicateWithFormat:@"content CONTAINS[cd] %@", _SearchBar.text]]];
Related