使用jsoup将html转换为纯文本时,如何保留换行符?

使用jsoup将html转换为纯文本时,如何保留换行符?,第1张

使用jsoup将html转换为纯文本时,如何保留换行符?

保留换行符的真正解决方案应该是这样的:

public static String br2nl(String html) {    if(html==null)        return html;    document document = Jsoup.parse(html);    document.outputSettings(new document.OutputSettings().prettyPrint(false));//makes html() preserve linebreaks and spacing    document.select("br").append("\n");    document.select("p").prepend("\n\n");    String s = document.html().replaceAll("\n", "n");    return Jsoup.clean(s, "", Whitelist.none(), new document.OutputSettings().prettyPrint(false));}

满足以下要求:

  1. 如果原始html包含换行符( n),则保留它
  2. 如果原始html包含br或p标签,它们将被翻译为换行符( n)。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5017587.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-15
下一篇 2022-11-15

发表评论

登录后才能评论

评论列表(0条)

保存