0

私はこのような文字列を持っています

<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>

この文字列から「src」( http://u.goal.com/136600/136687_thumb.jpg )を取得したい。どうすればそれを動的に取得できますか。

ありがとう!!

4

3 に答える 3

1

このように使用してURLを取得できます...

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]);
        }
    }

出力: http://u.goal.com/136600/136687_thumb.jpg

于 2013-05-16T10:28:45.653 に答える
0

これを使って :

NSString *aString = @"<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>";
NSRange r1 =[aString rangeOfString:@"src=\""];
NSRange r2 =[aString rangeOfString:@"\" style"];

NSRange rSub = NSMakeRange(r1.location + r1.length, r2.location - r1.location - r1.length);

NSString *subString = [aString substringWithRange:rSub];

お役に立てば幸いです。

于 2013-05-16T10:40:00.337 に答える
0

Stringあなたは2つの間のStringようなものを得ることができます

-(NSString*)stringBetweenString:(NSString*)start andString:(NSString)end {
    NSRange startRange = [self rangeOfString:start];
    if (startRange.location != NSNotFound) {
        NSRange targetRange;
        targetRange.location = startRange.location + startRange.length;
        targetRange.length = [self length] - targetRange.location;   
        NSRange endRange = [self rangeOfString:end options:0 range:targetRange];
        if (endRange.location != NSNotFound) {
           targetRange.length = endRange.location - targetRange.location;
           return [self substringWithRange:targetRange];
        }
    }
    return nil;
}
于 2013-05-16T10:26:18.780 に答える