[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])];
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])];
相关文章推荐
- iOS&nbsp;开发者必知的&nbsp;75&amp;nb…
- ios支付宝Demo报错(Linker&nbsp;co…
- [2014-10-18]iOS8开发错误整理(持…
- iOS开发之在地图上绘制出你运行的…
- 【2013-5-9】【iOS学习笔记】单独…
- 【2013-6-18】【iOS学习笔记】IOS…
- 【2013-8-15】【iOS学习笔记】傻瓜…
- [2014-02-17]自定义ios7标题栏和状…
- [2014-03-19]iOS7&nbsp;ARC和非ARC…
- [2014-03-19]iOS7&nbsp;ARC和非ARC…
- [2014-03-25]iOS&nbsp;NSString、…
- IOS7&nbsp;-系列译文:忘记N…
- [2014-03-27]iOS内置库POST异步
- ios5&nbsp;xcode&nbsp;4.3&nbsp;中&nbsp;release&nbsp;显示编…
- iOS&nbsp;SDK:&nbsp;How&nbsp;to&nbsp;creating&nbsp;an&nbsp;Awes…
- 【2012.08.06】iOS学习之JSON解析
- 【2012.08.24】iOS学习之同时创建n…
- 【2012.9.26】iOS客户端开发与Web…
- IOS学习网址之Core&nbsp;Animation入门
- IOS学习论坛