Java String trim 没有为我删除空白字符。
String rank = (some method);
System.out.println("(" + rank + ")");
输出是 (1)
。注意 1 右边的空格。
我必须从字符串 rank
中删除尾随空格,但 rank.trim()
和 rank.replace("","")
删除它。
字符串 rank
无论如何都保持不变。
编辑:完整代码::
Document doc = Jsoup.connect("http://www.4icu.org/ca/").timeout(1000000).get();
Element table = doc.select("table").get(7);
Elements rows = table.select("tr");
for (Element row: rows) {
String rank = row.select("span").first().text().trim();
System.out.println("("+rank+")");
}
为什么我不能删除那个空间?
最佳答案
该网站的源代码显示了特殊的 html 字符
。尝试在您的 java 字符串中搜索或替换以下内容:\u00A0
.
这是一个牢不可破的空间。见:I have a string with "\u00a0", and I need to replace it with "" str_replace fails
rank = rank.replaceAll("\u00A0", "");
应该可以。也许添加一个双 \\
而不是 \
。
关于Java字符串修剪没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11700598/