Search code examples
iosobjective-cnsstringnsrange

Get substring from string in iphone


I have a string like this

<img alt=\"Marco Bueno\" src=\"http://u.goal.com/136600/136687_thumb.jpg\" style=\"float: left;margin:0 10px 10px 10px;\" title=\"Marco Bueno\" /><p style=\"float:left;\">Herrera is currently on national team duty representing the U-23 side that has already made history at the Toulon Tournament, while Bueno won the U-17 World Cup in 2011</p>

I want to get the "src"(http://u.goal.com/136600/136687_thumb.jpg) from this string. How can i get that in a dynamic way.

Thanks!!


Solution

  • You can get your url using like this...

    NSRange divRange = [dateString rangeOfString:@"src=\"" options:NSCaseInsensitiveSearch];
        if (divRange.location != NSNotFound)
        {
            NSRange endDivRange;
    
            endDivRange.location = divRange.length + divRange.location;
            endDivRange.length   = [dateString length] - endDivRange.location;
            endDivRange = [dateString rangeOfString:@".jpg" options:NSCaseInsensitiveSearch range:endDivRange];
    
            if (endDivRange.location != NSNotFound)
            {
                divRange.location += divRange.length;
                divRange.length  = endDivRange.location - divRange.location + endDivRange.length;
    
    
                NSLog(@"BinarySecurityToken : %@",[dateString substringWithRange:divRange]);
            }
        }
    

    Output : http://u.goal.com/136600/136687_thumb.jpg