可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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 ?

回答1:

It depends what iOS version you are targeting. Since iOS7 there is a built-in method that will not only strip the HTML tags, but also put the formatting to the string:

Xcode 9/Swift 4 if let htmlStringData = htmlString.data(using: .utf8), let attributedString = try? NSAttributedString(data: htmlStringData, options: [.documentType : NSAttributedString.DocumentType.html], documentAttributes: nil) { print(attributedString) }

You can even create an extension like this: extension String { var htmlToAttributedString: NSAttributedString? { guard let data = self.data(using: .utf8) else { return nil } do { return try NSAttributedString(data: data, options: [.documentType : NSAttributedString.DocumentType.html, .characterEncoding: String.Encoding.utf8.rawValue], documentAttributes: nil) } catch { print("Cannot convert html string to attributed string: \(error)") return nil } } }

Note that this sample code is using UTF8 encoding. You can even create a function instead of computed property and add the encoding as a parameter.

Swift 3 let attributedString = try NSAttributedString(data: htmlString.dataUsingEncoding(NSUTF8StringEncoding)!, options: [NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType], documentAttributes: nil)

Objective-C [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:nil];

If you just need to remove everything between < and > (dirty way!!!), which might be problematic if you have these characters in the string, use this: - (NSString *)stringByStrippingHTML { NSRange r; NSString *s = [[self copy] autorelease]; while ((r = [s rangeOfString:@"]+>" options:NSRegularExpressionSearch]).location != NSNotFound) s = [s stringByReplacingCharactersInRange:r withString:@""]; return s; }

回答2:

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:&text] ; html = [html stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@>", text] withString:@""]; } // html = [html stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; return html; }

回答3:

For Swift Language , NSAttributedString(data:(htmlString as! String).dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: true )!, options:[NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: NSNumber(unsignedLong: NSUTF8StringEncoding)], documentAttributes: nil, error: nil)!

回答4:

- (NSString *)stringByStrippingHTML:(NSString *)inputString { NSMutableString *outString; if (inputString) { outString = [[NSMutableString alloc] initWithString:inputString]; if ([inputString length] > 0) { NSRange r; while ((r = [outString rangeOfString:@"]+>| " options:NSRegularExpressionSearch]).location != NSNotFound) { [outString deleteCharactersInRange:r]; } } } return outString; }

回答5:

Did you try something like that below, Not sure if it will faster as you did before using scanner please check:- //String which contains html tags NSString *htmlString=[NSString stringWithFormat:@"%@",@"right onto Kennington Park Rd/A3Continue to follow A3

c 读取html text,Converting HTML text into plain text using Objective-C相关推荐

  1. Plain Text

    Plain Text Plain Text一词可以翻译为纯文本,指的是用二进制编码表示字符的一种只包含文本信息的文件. 由于涉及到"编码"问题,我建议诸位读者不妨先读一读Charl ...

  2. mysql中text格式化_mysql中char,varchar,text

    1.char char最大长度是255字符,注意是字符数和字符集没关系. 1)可以有默认值, 2)尾部有空格会被截断 3)不管汉字.英文,还是其他编码,都可以存255字符 2.varchar 1)va ...

  3. Plain text, flat file, 及flat-file database 如何翻译

    经常遇到Plain text, flat file, 及flat-file database 这三个词,但不知翻译成什么样的中文好.请大家多指教. 英文意思我已经找到了: (1) plain text ...

  4. 首页静态生成 错误:客户端发现响应内容类型为“text/html”,但应该是“text/xml”...

    首页静态生成 错误:客户端发现响应内容类型为"text/html",但应该是"text/xml"解决方法: 最近在做首页静态生成,一直遇到这样的问题  客户端发 ...

  5. Android开发 第2课 控件TextView、Plain Text、ImageView、 Button、ImageButton以及点击事件

    控件 TextView 显示文本 Plain Text 输入文本 //将布局xml文件引入到activity当中setContentView(R.layout.activity_main);<! ...

  6. idea中文件被Mark as Plain Text后恢复

    一.idea中文件被标记:Mark as Plain Text 在idea中不小心把文件进行Mark as Plain Text标记后,会变成纯文本,如下图所示 ​​ 二.恢复 在工程目录上选择该文件 ...

  7. detecting text in natural image with connectionist text proposal network

    场景文字检测-CTPN原理与实现 - 知乎对于复杂场景的文字识别,首先要定位文字的位置,即文字检测.这一直是一个研究热点. Detecting Text in Natural Image with C ...

  8. endnote 进行 convert to plain text操作后word格式大变问题解决办法

    很简单,不要用convert to plain text这个操作去除参考文献的域代码,直接在word环境下 ctrl+shift+F9 就可以将域代码转化成文本,而非域代码的部分不会改变.

  9. 【Sublime text 3】卸载重装Sublime text 3英文版

    文章目录 问题描述 解决过程 问题描述 笔者之前所用Sublime text 3为中文版,发现不能安装插件的问题,现尝试卸载重装英文版. 解决过程 卸载关键: 在C盘搜索"sublime t ...

最新文章

  1. 367. Valid Perfect Square
  2. 导航控制器自定义返回控件及手势失效问题
  3. linux的临时页表映射问题
  4. 支持html5浏览器速查
  5. pythonsklearn乳腺癌数据集_使用sklearn的样本数据集
  6. 第二十六期:100 个网络基础知识普及,看完成半个网络高手
  7. 拼多多股价创历史新高:市值再度逼近京东
  8. Atitit.Gui控件and面板----数据库区-mssql 2008 权限 配置 报表查看成员
  9. 网络 TCP的头部 3次握手和4次挥手 出现大量TIME_WAIT或CLOSE_WAIT的原因及解决办法
  10. CCF NOI1069 分解因数
  11. 使用maven启动web项目报错
  12. iOS下载文件,保存路径. 防止加到iCloud备份
  13. java i 原理_Java I/O模型及其底层原理
  14. python之函数(function)
  15. 通讯录 C语言分类,C语言 通讯录
  16. python blp模型 估计_BLP模型
  17. 企业资源计划(ERP)原理与实践第一章
  18. windows域用户切换本地用户
  19. matlab留学生作业代做,代写Canvas留学生作业、MatLab编程语言作业调试、MatLab实验作业代做、代写program课程作业...
  20. 今天电脑突然出现问题: 请安装 TCP/IP 协议 错误 10106 【已解决·】

热门文章

  1. 字节老板在群里diss员工:上班时间聊游戏,工作很闲吗?员工回怼:查聊天记录,看聊天时间占工作时间百分比!网友:真·扁平化管理!...
  2. 基于Redis的分布式锁和Redlock算法
  3. 爬虫核心原理:一次 HTTP 请求到底是如何完成的?
  4. Hadoop数据收集与入库系统Flume与Sqoop
  5. 值得分享!它们才是真正的宝藏网站,让人惊艳
  6. 强烈推荐7个让人惊艳的宝藏实用网站,太好用了
  7. jQuery addClass,removeClass,class属性增删
  8. C++语言基本类型—浮点型
  9. 安装 esl php 模块,ESL系统的Docker化部署
  10. 有了ROS这架车,SLAM之路不再遥远!