我想解析一个字符串来替换一些元素。
如果我的字符串包含一个,<ol>
那么我想解析<li>
元素以用数字替换它们。
我知道我可以检查rangeOfString
包含的字符串。
if ([myString rangeOfString:@"<ol>"].location != NSNotFound) {
//Do stuff here
}
我怎样才能只解析<ol>
和</ol>
元素之间?
编辑1:这是我的解决方案
- (NSString *)parseOrderedList:(NSString *)str {
NSMutableString *html = [NSMutableString stringWithCapacity:[str length]];
NSScanner *scanner = [NSScanner scannerWithString:str];
NSString *tempText = nil;
while (![scanner isAtEnd])
{
[scanner scanUpToString:@"<ol>" intoString:&tempText];
[scanner scanUpToString:@"</ol>" intoString:&tempText];
if (tempText != nil) {
[html appendString:tempText];
html = [[[html stringByReplacingOccurrencesOfString:@"</li>" withString:@" "] mutableCopy] autorelease];
while ([html rangeOfString:@"<li>"].location != NSNotFound) {
html = [[[html stringByReplacingOccurrencesOfString:@"<li>" withString:[NSString stringWithFormat:@"%i.", i]] mutableCopy] autorelease];
i++;
}
}
if (![scanner isAtEnd])
[scanner setScanLocation:[scanner scanLocation] + 0];
tempText = @" ";
}
return html;
}
但是我该如何更换