Sunday, May 15, 2011

NSString rangeOfString example objc

NSString:rangeOfString checks whether the given NSString object contains a specified string and returns the starting and ending index of the substring in the NSString object. Following code fragment shows an example of NSString:rangeOfString method. 

NSString    *str = @"start_prefix  mystring  ending_suffix";
NSRange   range = [str  rangeOfString:@"mystring"];

if( range.location != NSNotFound )
{
    NSLog(@"The NSString contains a substring: mystring");
}