NSString – Getting a Substring

One thing I found different in Objective C was how getting a substring was done. If you have seen my previous posts you may have noticed this method.  I use it to grab a particular element value from XML without parsing the whole doc.  It also serves as a good example of how to get a substring from a NSString.

+(NSString *) getElementValue:(NSString * ) sSourceString: (NSString *) sStartTagValue : (NSString *) sEndTagValue
{
  NSString * subString = [[[NSString alloc] initWithString: @""] autorelease];

  @try
  {
    NSRange iStart  = [sSourceString rangeOfString :sStartTagValue];
    iStart.location += sStartTagValue.length;
    NSRange iEnd    = [sSourceString rangeOfString :sEndTagValue];
    int iLength     = iEnd.location - iStart.location;
    subString       = [sSourceString substringWithRange: NSMakeRange( iStart.location, iLength ) ];
  }
  @catch( NSException * ex){}

  return subString;
}