zoukankan      html  css  js  c++  java
  • iOS去掉字符串中的HTML标签的方法

    方法一、NSScanner去除标签

     1 - (NSString *)removeTheHtmlFromString:(NSString *)htmlString {
     2     NSScanner * scanner = [NSScanner scannerWithString:htmlString];
     3     NSString * text = nil;
     4     while([scanner isAtEnd]==NO) {
     5         //找到标签的起始位置
     6         [scanner scanUpToString:@"<" intoString:nil];
     7         //找到标签的结束位置
     8         [scanner scanUpToString:@">" intoString:&text];
     9         //替换字符
    10         htmlString = [htmlString stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>",text] withString:@""];
    11     }
    12     return htmlString;
    13 }

    方法二、正则方法

    1 //正则去除标签
    2 -(NSString *)removeHtmlWithString:(NSString *)htmlString{
    3     NSRegularExpression * regularExpretion=[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>|
    " options:0 error:nil];
    4     htmlString = [regularExpretion stringByReplacingMatchesInString:htmlString options:NSMatchingReportProgress range:NSMakeRange(0, htmlString.length) withTemplate:@""];
    5     return htmlString;
    6 }
  • 相关阅读:
    sublime插件时间
    git与github
    字符编码笔记:ASCII,Unicode和UTF-8
    阮一峰:互联网协议入门
    从理论到实践,全方位认识DNS
    ci事务
    linux下启动oracle
    Java连接Oracle
    我的博客终于开通了,加油!
    FILTER 执行次数
  • 原文地址:https://www.cnblogs.com/wanli-leon/p/11442604.html
Copyright © 2011-2022 走看看