You can use regular expressions for this:
NSRegularExpression* regex = [[NSRegularExpression alloc] initWithPattern:@"src=\"([^\"]*)\"" options:NSRegularExpressionCaseInsensitive error:nil];
NSString *text = @"this is the image <img src=\"http://vnexpress.net/Files/Subject/3b/bd/67/6f/chungkhoan-xanhdiem2.jpg\"> and it is very beautiful.";
NSArray *imgs = [regex matchesInString:text options:0 range:NSMakeRange(0, [text length])];
if (imgs.count != 0) {
NSTextCheckingResult* r = [imgs objectAtIndex:0];
NSLog(@"%@", [text substringWithRange:[r rangeAtIndex:1]]);
}
This regular expression is at the heart of the solution:
src="([^"]*)"
It matches the contents of the attribute srcand captures the contents between quotation marks (note the pair of parentheses). This header is then retrieved to [r rangeAtIndex:1]and used to retrieve the portion of the string you are looking for.
source
share