I have an input string in the format
“Jerry Lane”(angle bracket)jerry.lane@gmail.com(bracket closed),”Harry Potter”(angle bracket)harry.potter@gmail.com(bracket closed),”Indiana Jones”,(angle bracket)indiana.jones@gmail.com(bracket closed),”Tom Cruise”(angle bracket)tom.cruise@gmail.com(bracket closed)
Here, i am supposed to first separate the string on the basis of comma delimiter, which would give me a separate string like
“Jerry Lane”(angle bracket)jerry.lane@gmail.com(bracket closed)
Then i need to save extract the string between the <> brackets, which is essentially the string “jerry.lane@gmail.com”. I am using the following code, but it is giving me the following error:
Terminating app due to uncaught exception ‘NSRangeException’, reason: ‘-[__NSCFConstantString substringWithRange:]: Range or index out of bounds’
-(NSArray *)parseString:(NSString *)string
{
if(string)
{
NSArray *myArray = [string componentsSeparatedByString:@","];
for(NSMutableString *myString in myArray)
{
NSRange start,end;
start = [myString rangeOfString:@"<"];
end = [myString rangeOfString:@">"];
if(start.location != NSNotFound && end.location != NSNotFound)
{
NSString *emailAddress = [myString substringWithRange:NSMakeRange(start.location,end.location)];
NSString *name = [myString substringToIndex:start.location];
NSDictionary *myDictionary = [[NSDictionary alloc] init];
[myDictionary setValue:emailAddress forKey:@"Dhruvil Vyas"];
[testArray addObject:myDictionary];
}
}
}
return testArray;
}
The arguments that substring takes are the start position and the length
Not the start position and the end position.
More Info