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

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

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

瀏覽:92日期: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轉換為純文本內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Java
相關文章:
主站蜘蛛池模板: 冀州市| 绥化市| 叶城县| 旺苍县| 容城县| 龙川县| 手游| 理塘县| 比如县| 巴塘县| 中牟县| 鹿邑县| 游戏| 内乡县| 清苑县| 浦北县| 新和县| 巴楚县| 阿克苏市| 宜州市| 益阳市| 登封市| 牟定县| 敦煌市| 赣州市| 清丰县| 齐河县| 高唐县| 体育| 叙永县| 新乡市| 三原县| 闽清县| 禄丰县| 海兴县| 行唐县| 拜城县| 西贡区| 策勒县| 娄烦县| 黄大仙区|