码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-11-20 00:18:41      阅读:1327      评论:0      收藏:0      [点我收藏+]

标签:

猫猫分享,必须精品

原创文章,欢迎转载。转载请注明:翟乃玉的博客
地址:http://blog.csdn.net/u013357243

一:问题

技术分享

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

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

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

标签:

原文地址:http://blog.csdn.net/u013357243/article/details/49930569

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!