zoukankan      html  css  js  c++  java
  • 去掉NSString中的HTML标签

    经常出现字符串带有html标签。下面有个方法一步到位去掉HTML标签

    1. <span style="font-family: 'comic sans ms', sans-serif; color: #008080; font-size: medium;">+(NSString *)flattenHTML:(NSString *)html trimWhiteSpace:(BOOL)trim  
    2. {  
    3.     NSScanner *theScanner = [NSScanner scannerWithString:html];  
    4.     NSString *text = nil;  
    5.       
    6.     while ([theScanner isAtEnd] == NO) {  
    7.         // find start of tag  
    8.         [theScanner scanUpToString:@"<" intoString:NULL] ;  
    9.         // find end of tag  
    10.         [theScanner scanUpToString:@">" intoString:&text] ;  
    11.         // replace the found tag with a space  
    12.         //(you can filter multi-spaces out later if you wish)  
    13.         html = [html stringByReplacingOccurrencesOfString:  
    14.                 [ NSString stringWithFormat:@"%@>", text]  
    15.                                                withString:@""];  
    16.     }  
    17.       
    18.     return trim ? [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] : html;  
    19. }  
    20. </span>  

     调用方法:

     

        notification33.alertBody =[self flattenHTML:body trimWhiteSpace:YES];

     


  • 相关阅读:
    Struts2:<s:action>的使用
    Struts2:Struts2在jsp中使用标签时值的获取
    jsp:useBean的使用
    关于Filter的一点误解
    Strust2: 工作流程
    java程序连接MySQL数据库
    python 开发工具简介
    NCEP CFSR数据下载
    美国NOAA/AVHRR遥感数据
    气象网站
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3249207.html
Copyright © 2011-2022 走看看