iOS去掉字符串中的HTML标签的方法

方法一、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;
}
上一篇:收缩SQL Server数据库文件


下一篇:git清空某个文件的历史版本