去掉NSString中的HTML标签

来源:互联网 发布:矩阵奇异值和特征值 编辑:程序博客网 时间:2024/05/16 08:42

m

  1. (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. }  

调用方法:

 

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


0 0