您的位置:首页 > 移动开发 > IOS开发

[2014-04-20]ios获取数据中去掉htm…

2015-06-26 09:20 405 查看
最近在工作中遇到后台返回的json串带有html标签,为此找到了相应的方法去掉标签,目前测试所有标签均能去掉,如有不能的请告知。
html为json串中带有html标签的string类型。

NSScanner *theScanner = [NSScanner scannerWithString:html];

NSString *text = nil;

while ([theScanner isAtEnd] == NO) {

// find start of tag

[theScanner scanUpToString:@"<" intoString:NULL] ;

// find end of tag

[theScanner scanUpToString:@">" intoString:&text] ;

// replace the found tag with a space

//(you can filter multi-spaces out later if you wish)

html = [html stringByReplacingOccurrencesOfString:

[ NSString stringWithFormat:@"%@>", text]

withString:@""];

如果解析出来还带有\n \t \r之类的再在此基础上用""替代掉。如下:

NSMutableString *s = [NSMutableString
stringWithString: html];

[s
replaceOccurrencesOfString:@“ “ withString:@“”
options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s
length])];

[s
replaceOccurrencesOfString:@“\\r” withString:@“”
options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s
length])];

[s
replaceOccurrencesOfString:@“\t” withString:@“”
options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s
length])];

[s
replaceOccurrencesOfString:@“\\n” withString:@“”
options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s
length])];

[s
replaceOccurrencesOfString:@“+” withString:@“”
options:NSCaseInsensitiveSearch range:NSMakeRange(0, [s
length])];
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: