方法一、NSScanner去除标签
- (NSString *)removeTheHtmlFromString:(NSString *)htmlString {
NSScanner * scanner = [NSScanner scannerWithString:htmlString];
NSString * text = nil;
while([scanner isAtEnd]==NO) {
//找到标签的起始位置
[scanner scanUpToString:@"<" intoString:nil];
//找到标签的结束位置
[scanner scanUpToString:@">" intoString:&text];
//替换字符
htmlString = [htmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
}
return htmlString;
}
方法二、正则方法
//正则去除标签
-(NSString *)removeHtmlWithString:(NSString *)htmlString{
NSRegularExpression * regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n" options: error:nil];
htmlString = [regularExpretion stringByReplacingMatchesInString:htmlString options:NSMatchingReportProgress range:NSMakeRange(, htmlString.length) withTemplate:@""];
return htmlString;
}