如何将从 HTML 文件中解析的  
字符替换为“”?
关于此有很多问题和“已接受”的答案,例如,
How to remove with Jsoup?
How to remove " " from java string
Handling special entities like & nbsp; , & pound; in HtmlCleaner
但是我仍然没有通过使用这些链接中给出的所有解决方案来解决这个问题,
if(tr.text().replace("\u00a0", "").isEmpty()){
System.out.println("testing---");
}
我在这里做错了什么? jsoup可以用在这个场景吗?我在其中一个答案中读到了这一点。
更新 这是我试图从中获取数据的 HTML 部分。
<TR>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boBL noWrap> </TD>
<TD class=xl27boL noWrap align=right> </TD>
</TR>
最佳答案
answer given here工作正常。 isEmpty
得到 false
的原因是 tr
元素的文本不仅
个字符 - 其中还有其他空白字符。您需要添加 trim
调用:
if(tr.text().replace("\u00a0", "").trim().isEmpty()){
// Here --------------------------^^^^^^^
System.out.println("testing---");
}
当我将示例 HTML 运行到 JSoup 中时,我只是通过查看 tr.text() 返回的文本就弄清楚了这一点;基本调试。
关于java - 将   作为字符串处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25590720/