iOS去掉字符串中的HTML標籤的方法

方法1、NSScanner去除標籤html

 1 - (NSString *)removeTheHtmlFromString:(NSString *)htmlString {
 2     NSScanner * scanner = [NSScanner scannerWithString:htmlString];
 3     NSString * text = nil;
 4     while([scanner isAtEnd]==NO) {
 5         //找到標籤的起始位置
 6         [scanner scanUpToString:@"<" intoString:nil];
 7         //找到標籤的結束位置
 8         [scanner scanUpToString:@">" intoString:&text];
 9         //替換字符
10         htmlString = [htmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
11     }
12     return htmlString;
13 }

方法2、正則方法spa

1 //正則去除標籤
2 -(NSString *)removeHtmlWithString:(NSString *)htmlString{
3     NSRegularExpression * regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|\n" options:0 error:nil];
4     htmlString = [regularExpretion stringByReplacingMatchesInString:htmlString options:NSMatchingReportProgress range:NSMakeRange(0, htmlString.length) withTemplate:@""];
5     return htmlString;
6 }
相關文章
相關標籤/搜索