首页 > 解决方案 > Jsoup 去除字符串中的多个空格

问题描述

我正在尝试使用 jsoup.clean

    Whitelist.relaxed().preserveRelativeLinks(true);
    Jsoup.clean(html, wl);

我的 html 是一个字符串message = "This is a simple string messages with double spaces"

字符串中有两个空格。但是当我这样做时.clean(),它会修剪空格。我想保留这些空间。我该怎么做?事件.basic().simpleText()不起作用。

标签: javajsoupxss

解决方案


接受clean一个Document.outputSettings对象有一个重载:

public static String clean​(String bodyHtml, String baseUri, Safelist safelist, Document.OutputSettings outputSettings)

使用它,您可以设置 prettyPrint为 false:

公共布尔漂亮打印()

获取是否启用了漂亮的打印。默认为真。如果禁用,HTML 输出方法将不会重新格式化输出,并且输出通常看起来像输入。

所以你最终会得到这样的结果:

Whitelist.relaxed().preserveRelativeLinks(true);
Jsoup.clean(html, "", wl, new Document.OutputSettings().prettyPrint(false));

这应该让你得到你正在寻找的东西。


推荐阅读