zoukankan      html  css  js  c++  java
  • NSString去除所有HTML标签

    一般来说,干这种事正则才是王道。不过在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;  
    }
  • 相关阅读:
    英雄大乱斗
    深浅拷贝(copy)
    myleecode
    代码量简单统计
    pandas 模块 05
    matplotlib 模块 07
    KVC 和KVO浅谈
    iOS开发中懒加载的使用和限制
    关于空白模板插件的使用
    UIImageC处理
  • 原文地址:https://www.cnblogs.com/blogfantasy/p/4930916.html
Copyright © 2011-2022 走看看