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;  
    }
  • 相关阅读:
    C#操作Word (2)-- 打开&关闭Word文档
    JS input 银行卡号格式转换
    解决H5在微信浏览器或QQ浏览器修改title的问题
    CSS
    Atom 编辑器使用和学习
    php的一个小坑,输出不了json_encode
    js 组合键监听ctrl + enter
    webpack3.0 环境搭建
    css 使表格随着内容自动适应宽度
    获取input光标的x和y轴
  • 原文地址:https://www.cnblogs.com/blogfantasy/p/4930916.html
Copyright © 2011-2022 走看看