简体   繁体   English

Jsoup删除字符串中的多个空格

[英]Jsoup removes multiple whitespace in string

I am trying to use jsoup.clean with我正在尝试使用 jsoup.clean

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

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

There are double spaces in the string.字符串中有双空格。 But when I do .clean() , it trims the spaces.但是当我执行.clean() ,它会修剪空格。 I want to preserve these spaces.我想保留这些空间。 How do I do it?我该怎么做? Event .basic() and .simpleText() does not work.事件.basic().simpleText()不起作用。

There is an overload for clean that takes in a Document.outputSettings object: 有一个接受Document.outputSettings对象的clean 重载

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

Using that, you can set prettyPrint to false: 使用它,您可以将prettyPrint 设置为 false:

public boolean prettyPrint()公共布尔漂亮打印()

Get if pretty printing is enabled.获取是否启用漂亮打印。 Default is true.默认为真。 If disabled, the HTML output methods will not re-format the output, and the output will generally look like the input.如果禁用,HTML 输出方法将不会重新格式化输出,并且输出通常看起来像输入。

So you end up with something like this:所以你最终会得到这样的结果:

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

That should get you what you're looking for.那应该让你得到你正在寻找的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM