Sunday, May 19, 2013

NSString rangeOfString options range example ios


[NSString rangeOfString options range]

Finds and returns the range of the first occurrence of a given string, within the given range of the receiver, subject to given options.
- (NSRange)rangeOfString:(NSString *)aString options:(NSStringCompareOptions)mask range:(NSRange)aRange
Parameters of [NSString rangeOfString options range]
aString
The string for which to search. 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,NSLiteralSearchNSBackwardsSearch, and NSAnchoredSearch. See String Programming Guide for details on these options.
aRange
The range within the receiver for which to search for aString.

Important: Raises an NSRangeException if any part of aRange lies beyond the end of the string.
Return Value of [NSString rangeOfString options range]
An NSRange structure giving the location and length in the receiver of aString withinaRange in the receiver, modulo the options in mask. The range returned is relative to the start of the string, not to the passed-in range. Returns {NSNotFound, 0} ifaString is not found or is empty (@"").
Discussion of [NSString rangeOfString options range]
The length of the returned range and that of aString may differ if equivalent composed character sequences are matched.
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 range] example
// Find all ssid
while (keepGoing) {
    NSRange accessWord = [strippedString rangeOfString:@"ACCESSKEY" options:NSCaseInsensitiveSearch range:searchRange];
    if (accessWord.location != NSNotFound) {
        // since we have found the access key, we can assume somethings
        // ACCESSKEY: FA3540B52F
        int pos = accessWord.location + 11;
        NSString *ssid = [strippedString substringWithRange:NSMakeRange(pos, pos + 10)];

        NSLog(@"SSID: %@", ssid);

        // reset our search up a little for our next loop around
        searchRange = NSMakeRange(pos, [strippedString length] - pos);
    } else {
        keepGoing = NO;
    }
}
[NSString rangeOfString options range] example
- (NSRange)rangeOfString:(NSString *)substring
                inString:(NSString *)string
             atOccurence:(int)occurence
{
  int currentOccurence = 0;
  NSRange rangeToSearchWithin = NSMakeRange(0, string.length);

  while (YES)
  {
    currentOccurence++;
    NSRange searchResult = [string rangeOfString: substring
                                         options: NULL
                                           range: rangeToSearchWithin];

    if (searchResult.location == NSNotFound)
    {
      return searchResult;
    }
    if (currentOccurence == occurence)
    {
      return searchResult;
    }

    int newLocationToStartAt = searchResult.location + searchResult.length;
    rangeToSearchWithin = NSMakeRange(newLocationToStartAt, string.length - newLocationToStartAt);
  }
}
[NSString rangeOfString options range] 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);

            }
        }
    }