Monday, May 20, 2013

NSString substringWithRange example ios


[NSString substringWithRange]

Returns a string object containing the characters of the receiver that lie within a given range.
- (NSString *)substringWithRange:(NSRange)aRange
Parameters
aRange
A range. The range must not exceed the bounds of the receiver.

Important: Raises an NSRangeException if any part of aRange lies beyond the end of the receiver.
Return Value
A string object containing the characters of the receiver that lie within aRange.
Discussion of [NSString substringWithRange]
This method treats the length of the string as a valid range value that returns an empty string.
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.
Example of [NSString substringWithRange]
int startPosition = [mainString rangeOfString:@"("].location + 1;
int endPosition   = [mainString rangeOfString:@")"].location;

NSRange range = NSMakeRange(startPosition, endPosition - startPosition);

NSString *subString = [mainString substringWithRange:range];
Example of [NSString substringWithRange]
NSString *foo = @"value:hello World:value";
NSString *value = [foo substringWithRange:NSMakeRange(6, [foo length] - 2 * 6)];
//This assumes that all strings of this format have the prefix "value:" and the suffix ":value".
//6 is the length of both your prefix and your suffix
Example of [NSString substringWithRange]
- (NSString *)stringByExpandingTemplateWithVariables:(NSDictionary *)dictionary
{
    NSUInteger myLength = self.length;
    NSMutableString *result = [NSMutableString stringWithCapacity:myLength];

    NSRange remainingRange = NSMakeRange(0, myLength);
    while (remainingRange.length > 0) {
        NSRange leftBraceRange = [self rangeOfString:@"{" options:0 range:remainingRange];
        if (leftBraceRange.location == NSNotFound)
            break;

        NSRange afterLeftBraceRange = NSMakeRange(NSMaxRange(leftBraceRange), myLength - NSMaxRange(leftBraceRange));
        NSRange rightBraceRange = [self rangeOfString:@"}" options:0 range:afterLeftBraceRange];
        if (rightBraceRange.location == NSNotFound)
            break;

        NSRange beforeLeftBraceRange = NSMakeRange(remainingRange.location, leftBraceRange.location - remainingRange.location);
        [result appendString:[self substringWithRange:beforeLeftBraceRange]];
        remainingRange = NSMakeRange(NSMaxRange(rightBraceRange), myLength - NSMaxRange(rightBraceRange));

        NSRange keyRange = NSMakeRange(NSMaxRange(leftBraceRange), rightBraceRange.location - NSMaxRange(leftBraceRange));
        NSString *key = [self substringWithRange:keyRange];
        NSString *value = [dictionary objectForKey:key];
        if (value)
            [result appendString:value];
    }

    [result appendString:[self substringWithRange:remainingRange]];
    return result;
}