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];

     


  • 相关阅读:
    git一个项目导入MyEclipse上传到github
    python之列表练习
    python之元组
    python之列表
    python之替换文件内容和修改某行内容
    python之查询员工名单(读取文件,查询文件内容)
    js:函数一
    8.1(java学习笔记)注解(Annotation)
    8.2(java学习笔记)反射
    7.4 (java学习笔记)网络编程之TCP
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3249207.html
Copyright © 2011-2022 走看看