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

NSString去除所有HTML标签

时间:2015-11-02 19:30:34      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

一般来说,干这种事正则才是王道。不过在ios上执行的效率貌似也高不了多少(第三方类库是在Obj-C的基础上写的),所以弄个一般的方法,基本满足需求就可以了。

废话不多说,直接上代码(google上能找到很多一样的代码)
- (NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim {
    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:@""];
    }
 
    // trim off whitespace
    return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;  
}

NSString去除所有HTML标签

标签:

原文地址:http://www.cnblogs.com/blogfantasy/p/4930916.html

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