国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術文章
文章詳情頁

在Java中輕松將HTML格式文本轉換為純文本的方法示例(保留換行)

瀏覽:242日期:2022-09-02 18:42:15

第一步:引入Jsoup和lang和lang3的依賴:

Jsoup是HTML解析器lang和lang3這兩個包里有轉換所需的工具類

<dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.11.3</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>org.apache.commons</groupId><artifactId>commons-lang3</artifactId><version>3.4</version></dependency>

第二步:直接使用即可:

import org.apache.commons.lang.StringEscapeUtils;import org.apache.commons.lang3.StringUtils;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.safety.Whitelist;/** * @author Piconjo */public class Html2PlainText { public static String convert(String html) { if (StringUtils.isEmpty(html)) { return ''; } Document document = Jsoup.parse(html); Document.OutputSettings outputSettings = new Document.OutputSettings().prettyPrint(false); document.outputSettings(outputSettings); document.select('br').append('n'); document.select('p').prepend('n'); document.select('p').append('n'); String newHtml = document.html().replaceAll('n', 'n'); String plainText = Jsoup.clean(newHtml, '', Whitelist.none(), outputSettings); String result = StringEscapeUtils.unescapeHtml(plainText.trim()); return result; }}

使用測試:

在Java中輕松將HTML格式文本轉換為純文本的方法示例(保留換行)

在Java中輕松將HTML格式文本轉換為純文本的方法示例(保留換行)

到此這篇關于在Java中輕松將HTML格式文本轉換為純文本的方法示例(保留換行)的文章就介紹到這了,更多相關Java HTML轉換為純文本內容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持好吧啦網(wǎng)!

標簽: Java
相關文章:
主站蜘蛛池模板: 阜宁县| 柘城县| 胶南市| 安康市| 高要市| 讷河市| 新余市| 监利县| 延安市| 黑河市| 苏尼特左旗| 资中县| 红原县| 台南市| 泰兴市| 正定县| 遂川县| 班玛县| 大同县| 平南县| 岫岩| 沈丘县| 昌宁县| 深州市| 黄浦区| 博兴县| 桐城市| 青阳县| 繁峙县| 册亨县| 措勤县| 德阳市| 满洲里市| 阜平县| 麦盖提县| 延川县| 阳原县| 灯塔市| 兴安县| 宁晋县| 九寨沟县|