java - 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()
不起作用。
解决方案
接受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));
这应该让你得到你正在寻找的东西。
推荐阅读
- kotlin - 如何仅返回 Kotlin 中两个并发异步挂起函数中的第一个的结果?
- java - 如何使用 Spring Boot 调度程序获取价格差异
- laravel - 如何在 Spatie Laravel 包中制作多个角色?
- swift - URLSession:使用 http/1.1 而不是 http/2
- r - 具有多个条件和 NA 的 ifelse 语句
- java - Java + Postgresql:如何在 HashMap 中存储所有子项的父项和列表
> - css - 学习者问题:当窗口缩小时,如何使用 CSS 使按钮留在容器内。SPFX 网络部件
- pandas - Pandas:如何将“年-月”等格式更改为 datetime64 格式?
- php - 如何在php中使用for循环创建变量
- reactjs - 如何使用 react router 实现逐步导航(如幻灯片)?