简体   繁体   English

从iPhone中的字符串获取子字符串

[英]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. 我想从该字符串中获取“ src”( http://u.goal.com/136600/136687_thumb.jpg )。 How can i get that in a dynamic way. 我如何以一种动态的方式得到它。

Thanks!! 谢谢!!

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 输出: http : //u.goal.com/136600/136687_thumb.jpg

you can get the String between two String like 你可以得到的String两者之间的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;
}

Use this : 用这个 :

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

Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM