There are times during development when you need to match text against a search phrase. NSString offers a few simple built-in routines that offer basic comparison options. For example, use isEqualToString: to compare two strings for equality. Often, you need more flexible searching. NSString can take you along that path, but only so far. NSString lets you do the following:
- You can test if a string hasPrefix: or hasSuffix: to find matches at the start or end of your string.
- When the range returned by rangeOfString: has its location set to NSNotFound, you know the two strings do not match.
- NSString also offers a number of comparison functions like compare: and caseInsensitiveCompare:, which will show you whether the strings are in ascending or descending order.
For more advanced string matching consider using NSPredicates. This class, which supports a huge range of comparison options, offers both substring and regular expression matching. Here are a couple of examples that match a string to a phrase using NSPredicates:
- BOOL checkmatch(NSString *item, NSString *matchphrase)
- {
- BOOL match = NO;
- NSPredicate *containPred = [NSPredicate predicateWithFormat:@"SELF contains[cd] %@", matchphrase];
- match = match | [containPred evaluateWithObject:item];
- NSPredicate *matchPred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", matchphrase];
- match = match | [matchPred evaluateWithObject:item];
- return match;
- }
The first example matches based on a set substring. If the matchphrase appears in the string, the predicate evaluates to true. The two parameters in the square brackets modify the "contains" request. The c means match in a case insensitive manner; the d refers to a diacritic insensitive lookup.
The second example performs a regular expression match. This match is evaluated against the entire string. If you search for ".a", you would match "Ba" but not "Bologna." To match both, search for ".*a" instead.
Predicates can also help match against strings in arrays. Recently, I needed to recover arguments for a command-line utility. Filtering my predicate helped me recover only those arguments that were flags. Here's the code I used to search for items that started with "-".
- NSArray *args = [[NSProcessInfo processInfo] arguments];
- NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF beginswith '-'"];
- NSArray *dashedArgs = [args filteredArrayUsingPredicate:pred];
This search used a style of predicate that searched for strings that began with a matching item. When applied against my array, it filtered out just those items that matched my request, producing my list of flags.
Apple offers an in-depth guide to creating and using predicates. This construct can go far beyond simple string comparisons, allowing you to compare complex structures and objects, and it's a great tool to have on-hand even when you are just working with strings. Predicates simplify string comparison functions, allowing you to build general match tests with very few lines of readable and easy-to-maintain code.