使用Objective-C将HTML文本转换为纯文本 [英] Converting HTML text into plain text using Objective-C
本文介绍了使用Objective-C将HTML文本转换为纯文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有巨大的 NSString
里面有HTML文本。该字符串的长度超过3.500.000个字符。我怎样才能将这个HTML文本转换为 NSString
内部的纯文本。我使用的是扫描仪,但效果太慢。任何想法?
I have huge NSString
with HTML text inside. The length of this string is more then 3.500.000 characters. How can i convert this HTML text to NSString
with plain text inside. I was using scanner , but it works too slowly. Any idea ?
推荐答案
我使用扫描仪解决了我的问题,但我并不是将它用于所有文本。我将它用于每个10.000文本部分,然后将所有部分拼接在一起。我的代码如下
I resolve my question with scanner, but i use it not for all the text. I use it for every 10.000 text part, before i concatenate all parts together. My code below
-(NSString *)convertHTML:(NSString *)html {
NSScanner *myScanner;
NSString *text = nil;
myScanner = [NSScanner scannerWithString:html];
while ([myScanner isAtEnd] == NO) {
[myScanner scanUpToString:@"<" intoString:NULL] ;
[myScanner scanUpToString:@">" intoString:&text] ;
html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""];
}
//
html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
return html;
}
这篇关于使用Objective-C将HTML文本转换为纯文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文