Monday, May 20, 2013

NSString NSLiteralSearch example ios


Search and Comparison Options - NSLiteralSearch

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
};
Constants
NSCaseInsensitiveSearch
A case-insensitive search.
NSLiteralSearch
Exact character-by-character equivalence.
NSBackwardsSearch
Search from end of source string.[NSString NSLiteralSearch]
NSAnchoredSearch
Search is limited to start (or end, if NSBackwardsSearch) of source string.
NSNumericSearch
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.
NSDiacriticInsensitiveSearch
Search ignores diacritic marks.[NSString NSLiteralSearch]
For example, ‘รถ’ is equal to ‘o’.
NSWidthInsensitiveSearch
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).
NSForcedOrderingSearch
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.
NSRegularExpressionSearch
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 NSLiteralSearch]
See “Searching, Comparing, and Sorting Strings” for details on the effects of these options.


Example of [NSString NSLiteralSearch]

- (void)findAllLocationsOfString:(NSString *)substring sourceString:(NSString *)source
{
    NSRange searchRange = NSMakeRange(0, [source length]);
    NSRange place = NSMakeRange(0, 0);
    while (searchRange.location < [source length])
    {
        place = [source rangeOfString:substring options:NSLiteralSearch range:searchRange];
        if (place.location != NSNotFound) NSLog(@"found here : %d", place.location);
        searchRange.location = place.location + place.length;
        searchRange.length = [source length] - searchRange.location;
    }
}

Example of [NSString NSLiteralSearch]
[myMutableString replaceOccurrencesOfString:@"target" withString:@"replacement" options: NSLiteralSearch range: NSMakeRange(0, [myMutableString length])];
Example of [NSString NSLiteralSearch]
   NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *s1 = @"foo";
    NSString *s2 = @"foo";
    NSString *s3 = [[[NSString alloc] initWithString:@"foo"] autorelease];
    NSMutableString *s4 = [NSMutableString stringWithString:@"foobar"];
    [s4 replaceOccurrencesOfString:@"bar"
                        withString:@""
                           options:NSLiteralSearch
                             range:NSMakeRange(0, [s4 length])];

    NSLog(@"s1 = %p\n", s1);
    NSLog(@"s2 = %p\n", s2);
    NSLog(@"s3 = %p\n", s3);
    NSLog(@"s4 = %p\n", s4); // distinct from s1

    NSLog(@"%i", [s1 isEqualToString:s4]); // 1

    [pool release];