保留换行符的真正解决方案应该是这样的:
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));}
满足以下要求:
- 如果原始html包含换行符( n),则保留它
- 如果原始html包含br或p标签,它们将被翻译为换行符( n)。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)