Sunday, May 19, 2013

NSString rangeOfString options example ios


[NSString rangeOfString options ]

Finds and returns the range of the first occurrence of a given string within the receiver, subject to given options.
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask
Parameters of [NSString rangeOfString options ]
aString
The string to search for. This value must not be nil.

Important: Raises an NSInvalidArgumentException if aString is nil.
mask
A mask specifying search options. The following options may be specified by combining them with the C bitwise OR operator: NSCaseInsensitiveSearch,NSLiteralSearchNSBackwardsSearchNSAnchoredSearch. See String Programming Guide for details on these options.
Return Value of [NSString rangeOfString options ]
An NSRange structure giving the location and length in the receiver of the first occurrence of aString, modulo the options in mask. Returns {NSNotFound, 0} ifaString is not found or is empty (@"").
Discussion of [NSString rangeOfString options ]
Invokes rangeOfString:options:range: with the options specified by mask and the entire extent of the receiver as the range.
This method detects all invalid ranges (including those with negative lengths). For applications linked against OS X v10.6 and later, this error causes an exception; for applications linked against earlier releases, this error causes a warning, which is displayed just once per application execution.
[NSString rangeOfString options ] example
NSString *string = @"{Hello} ({World}) ({How}) ({Are}) ({You})";
    NSMutableString *result = [[NSMutableString alloc] init];

    NSArray *tempArray = [[string componentsSeparatedByString:@" "] mutableCopy];

    for (int i=0; i < [tempArray count]; i++)
    {
        NSString *tempStr = [tempArray objectAtIndex:i];
        NSRange startRange = [tempStr rangeOfString:@"{" options:NSCaseInsensitiveSearch];
        if (startRange.location != NSNotFound)
        {
            // Determine "}" location according to "{" location
            NSRange endRange;

            endRange.location = startRange.length + startRange.location;
            endRange.length   = [tempStr length] - endRange.location;
            endRange = [tempStr rangeOfString:@"}" options:NSCaseInsensitiveSearch range:endRange];

            if (endRange.location != NSNotFound)
            {
                // bracets found: retrieve string between them
                startRange.location += startRange.length;
                startRange.length = endRange.location - startRange.location;

                //result = [tempStr substringWithRange:startRange];
                [result appendString:[NSString stringWithFormat:@"%@ ",[tempStr substringWithRange:startRange]]];
                NSLog(@"%@ ",result);

            }
        }
    }
[NSString rangeOfString options ] example
NSString *string = @"hello BLA";
if ([string rangeOfString:@"bla" options:NSCaseInsensitiveSearch].location == NSNotFound) {
    NSLog(@"string does not contain bla");
} else {
    NSLog(@"string contains bla!");
}
[NSString rangeOfString options ] example
NSString *string = @"hello bla bla";

if ([string rangeOfString:@"BLA" options:NSCaseInsensitiveSearch].location == NSNotFound)
{
    NSLog(@"string does not contain bla");
} 
else 
{
    NSLog(@"string contains bla!");
}