IOS去掉字符串中HTML标签的方法
后台返回的字符串中带HTML标签,如果不用webView加载解析的话,就直接去掉。
1 -(NSString *)filterHTML:(NSString *)html 2 { 3 NSScanner * scanner = [NSScanner scannerWithString:html]; 4 NSString * text = nil; 5 while([scanner isAtEnd]==NO) 6 { 7 //找到标签的起始位置 8 [scanner scanUpToString:@"<" intoString:nil]; 9 //找到标签的结束位置 10 [scanner scanUpToString:@">" intoString:&text]; 11 //替换字符 12 html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""]; 13 } 14 // NSString * regEx = @"<([^>]*)>"; 15 // html = [html stringByReplacingOccurrencesOfString:regEx withString:@""]; 16 return html; 17 }