我有一个包含此内容的字符串:
href="http://www.website.com/" /> [...] [...]
我只想获得字符串的中心部分。怎么可能得到@“href =”“和@”“/>”
之间的部分注意:即使它看起来像xml-code,它也在NSString中。
答案 0 :(得分:10)
我明白了!
这是代码(写得不是很好):
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];
答案 1 :(得分:2)
怎么样
NSString* newNSString =[[[theString substringFromIndex:6] componentsSeparatedByString:@"/\""]objectAtIndex:0];
或者
NSString* newNSString =[[[[theString componentsSeparatedByString:@"href=\""]objectAtIndex:1] componentsSeparatedByString:@"/\""]objectAtIndex:0];
答案 2 :(得分:1)
这可以更紧凑地完成:
NSRange end = [source rangeOfString:@"\" />"];
if (end.location != NSNotFound) {
result = [source substringWithRange:NSMakeRange(6, end.location - 6)];
}