i want to parse a string for replacing some elements.
if my string contains an <ol> then i want to parse for the <li> elements to replace them with numbers.
I know that i can check with rangeOfString for the containing String.
if ([myString rangeOfString:@"<ol>"].location != NSNotFound) {
//Do stuff here
}
How can i parse only between <ol> and </ol> elements?
EDIT 1: Here is my solution
- (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;
}
But how can i replace the
Do this
U have string in nsmutablearray which can used to replace by your string
Same method can be repeated for other tags