猫猫学iOS之去除服务器返回数据中的html标签,去除指定字符串,替换字符串_html/css_WEB-ITnose

WBOY
Release: 2016-06-24 11:33:18
Original
1061 people have browsed it

猫猫分享,必须精品

一:问题

如图中,服务器返回的数据里面有大串的html 但是我们只用字符串,由于不想麻烦后台修改数据。。。。(喵很为别人着想)于是自己想办法解决。

其实解决的方法很多很多。。比如用字符串的截取方法的到range,然后根据位置来得到里面的想要的东东。。嘎的,想想都崩溃。
还有呢用正则表达式等等。。。正则表达式,说实话这东西除了面试时候说说和学习时候用过做项目还从来没有自己写过,pass,于是网上搜索学习,得到了一个方法,共享给大家。

二:解决

//去掉html标签-(NSString *)flattenHTML:(NSString *)html {    NSScanner *theScanner;    NSString *text = nil;    theScanner = [NSScanner scannerWithString:html];    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:@""];    }    return html;}
Copy after login

恩,就是上面的方法,他会把那些标签方法(带着的)直接替换成了@”” 空格,然后返还回来的就是了。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template