iOS字符串NSString 过滤HTML标签的两种方法
2014-09-06 15:43
671 查看
//第一种,用NSScanner扫描,来自下面这个著名的链接,不过现在打不开鸟~
// Source: http://rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c
- (NSString *)removeHTML:(NSString *)html {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:html];
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:@" "];
}
return html;
}
//第二种,用NSString自带的Seprated自截断方法
- (NSString *)removeHTML2:(NSString *)html{
NSArray *components = [html componentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];
NSMutableArray *componentsToKeep = [NSMutableArray array];
for (int i = 0; i < [components count]; i = i + 2)
{
[componentsToKeep addObject:[components objectAtIndex:i]];
}
NSString *plainText = [componentsToKeep componentsJoinedByString:@""];
return plainText;
}
// Source: http://rudis.net/content/2009/01/21/flatten-html-content-ie-strip-tags-cocoaobjective-c
- (NSString *)removeHTML:(NSString *)html {
NSScanner *theScanner;
NSString *text = nil;
theScanner = [NSScanner scannerWithString:html];
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:@" "];
}
return html;
}
//第二种,用NSString自带的Seprated自截断方法
- (NSString *)removeHTML2:(NSString *)html{
NSArray *components = [html componentsSeparatedByCharactersInSet:[NSCharacterSetcharacterSetWithCharactersInString:@"<>"]];
NSMutableArray *componentsToKeep = [NSMutableArray array];
for (int i = 0; i < [components count]; i = i + 2)
{
[componentsToKeep addObject:[components objectAtIndex:i]];
}
NSString *plainText = [componentsToKeep componentsJoinedByString:@""];
return plainText;
}
相关文章推荐
- Objective c里字符串NSString 过滤HTML标签的两种方法
- Objective c里字符串NSString 过滤HTML标签的两种方法
- Objective-C里字符串NSString过滤HTML标签的方法
- IOS去掉字符串中的html标签的方法
- ios去掉字符串中的html标签的方法
- iOS开发 字符串过滤html标签
- 过滤字符串html标签方法
- ios去掉字符串中的html标签的方法
- iOS NSString如何过滤html标签
- iOS开发——Swift字符串替换 + HTML标签正则过滤 + 主线程非阻断延时
- IOS中过滤HTML标签&&WebView的三种常用的使用方法
- asp.net 截取带有html标签的字符串(先过滤html,再截取)的方法
- 网页内容的html标签补全和过滤的两种方法
- ios去掉字符串中的html标签的方法
- 网页内容的html标签补全和过滤的两种方法
- 去除HTML代码中所有标签的两种方法
- DEDE中如何过滤掉Html标签,并且截取字符串长度
- 浅析php过滤html字符串,防止SQL注入的方法
- ios字符串过滤表情符号的方法
- ASP.NET过滤html标签的几种常用方法