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;  
    }
  • 相关阅读:
    d3-tree 双向树
    .gitignore
    url正则匹配
    this 指向
    git 用法小总结
    心态崩了?
    内存溢出和内存泄漏的区别
    jQuery添加方法
    物理像素与逻辑像素
    服务器返回的status
  • 原文地址:https://www.cnblogs.com/blogfantasy/p/4930916.html
Copyright © 2011-2022 走看看