Search for a string between two known strings

前端 未结 3 941
闹比i
闹比i 2021-01-03 11:48

I have a String with this content:

href=\"http://www.website.com/\" /> [...] [...]

I just want to get the central part of the string. Ho

相关标签:
3条回答
  • 2021-01-03 12:35

    I got it!

    This is the code (not very nice written):

    NSRange rr2 = [content rangeOfString:@"href=\""];
        NSRange rr3 = [content rangeOfString:@"\" />"];
        int lengt = rr3.location - rr2.location - rr2.length;
        int location = rr2.location + rr2.length;
        NSRange aa;
        aa.location = location;
        aa.length = lengt;
        theString = [theString substringWithRange:aa];
    
    0 讨论(0)
  • 2021-01-03 12:36

    This can be done more compactly:

    NSRange end = [source rangeOfString:@"\" />"];
    if (end.location != NSNotFound) {
        result = [source substringWithRange:NSMakeRange(6, end.location - 6)];
    }
    
    0 讨论(0)
  • 2021-01-03 12:47

    How about

    NSString* newNSString =[[[theString substringFromIndex:6] componentsSeparatedByString:@"/\""]objectAtIndex:0];
    

    Or

    NSString* newNSString =[[[[theString componentsSeparatedByString:@"href=\""]objectAtIndex:1] componentsSeparatedByString:@"/\""]objectAtIndex:0];
    
    0 讨论(0)
提交回复
热议问题