Monday, May 20, 2013

NSString NSDiacriticInsensitiveSearch example ios

Search and Comparison Options - NSDiacriticInsensitiveSearch

These values represent the options available to many of the string classes’ search and comparison methods.
enum {
   NSCaseInsensitiveSearch = 1,
   NSLiteralSearch = 2,
   NSBackwardsSearch = 4,
   NSAnchoredSearch = 8,
   NSNumericSearch = 64,
   NSDiacriticInsensitiveSearch = 128,
   NSWidthInsensitiveSearch = 256,
   NSForcedOrderingSearch = 512,
   NSRegularExpressionSearch = 1024
A case-insensitive search.
Exact character-by-character equivalence.
Search from end of source string.[NSString NSDiacriticInsensitiveSearch]
Search is limited to start (or end, if NSBackwardsSearch) of source string.
Numbers within strings are compared using numeric value, that is, Name2.txt <Name7.txt < Name25.txt.
This option only applies to compare methods, not find.
Search ignores diacritic marks.[NSString NSDiacriticInsensitiveSearch]
For example, ‘ö’ is equal to ‘o’.
Search ignores width differences in characters that have full-width and half-width forms, as occurs in East Asian character sets.
For example, with this option, the full-width Latin small letter 'a' (Unicode code point U+FF41) is equal to the basic Latin small letter 'a' (Unicode code point U+0061).
Comparisons are forced to return either NSOrderedAscending orNSOrderedDescending if the strings are equivalent but not strictly equal.
This option gives stability when sorting. For example, “aaa” is greater than "AAA” if NSCaseInsensitiveSearch is specified.
The search string is treated as an ICU-compatible regular expression. If set, no other options can apply except NSCaseInsensitiveSearch and NSAnchoredSearch. You can use this option only with the rangeOfString:...methods and stringByReplacingOccurrencesOfString:withString:options:range:.
Discussion of [NSString NSDiacriticInsensitiveSearch]
See “Searching, Comparing, and Sorting Strings” for details on the effects of these options.

Example of [NSString NSDiacriticInsensitiveSearch]

// should return NSOrderedSame, i.e. identical
[@"Apple" compare:@"Ápple" 
If you want it to be case-insensitive as well:
// ditto
[@"Apple" compare:@"Ápple" 
          options:NSDiacriticInsensitiveSearch | NSCaseInsensitiveSearch] 
Example of [NSString NSDiacriticInsensitiveSearch]
NSString *accentedString = @"ÁlgeBra";
NSString *unaccentedString = [accentedString stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:[NSLocale currentLocale]];
NSString *capitalizedString = [unaccentedString capitalizedString];
Example of [NSString NSDiacriticInsensitiveSearch]
NSStringCompareOptions  compareOptions = NSDiacriticInsensitiveSearch;
NSArray* countryIndex = [[NSArray alloc] initWithObjects:@"alpha", @"beta", @"gamma' nil];

for (NSString* element in countryIndex) {
    NSInteger result = [(NSString *)country compare:element options:compareOptions];
    if(NSOrderedAscending == result) {
        // Do something here...
    else if (NSOrderedSame == result) {
        // Do another thing here if they match...
    else {
        // Try something else...