当前位置:Gxlcms > html代码 > 猫猫学iOS之去除服务器返回数据中的html标签,去除指定字符串,替换字符串_html/css_WEB-ITnose

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

时间:2021-07-01 10:21:17 帮助过:33人阅读

猫猫分享,必须精品

一:问题

如图中,服务器返回的数据里面有大串的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;}

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

人气教程排行