我正在尝试删除 html 元素文本的额外空间,但它不起作用。 原来的html元素是:
<td class="thinborder" > text</td>
如果我使用 Jsoup 的 element.text() 访问这个,它会返回:
" text"
以下是我使用过的方法,但都不起作用:
String fixed = element.text().replace(" ", "");
String fixed = element.text().replace("nbsp;", "");
String fixed = element.text().replaceFirst(" ", "");
编辑: *修剪也不起作用
最佳答案
就trim()
字符串
String fixed = element.text().trim();
Returns a copy of the string, with leading and trailing whitespace omitted.
还可以尝试正则表达式
String result = element.text().replaceAll("\\s+","");
如果您仍然无法让它工作,请尝试替换 unicode 空间
String result = element.text().replace("\u00A0","");
关于java - 从 HTML 替换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20862067/