Java String trim无效 [英] Java String trim has no effect
问题描述
Java String trim不会为我删除空格字符。
Java String trim is not removing a whitespace character for me.
String rank = (some method);
System.out.println("(" + rank + ")");
输出(1)
。注意1右边的空格。
The output is (1 )
. Notice the space to the right of the 1.
我必须从字符串 rank
中删除尾随空格但是既不 rank.trim()
也不 rank.replace(,)
将其删除。
I have to remove the trailing space from the string rank
but neither rank.trim()
nor rank.replace(" ","")
removes it.
字符串 rank
两种方式都保持不变。
The string rank
just remains the same either way.
编辑:完整代码::
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+")");
}
为什么我不能删除该空格?
Why can't I remove that space?
推荐答案
该网站的源代码显示特殊的html字符& nbsp;
。尝试在java字符串中搜索或替换以下内容: \ u00A0
。
The source code of that website shows the special html character
. Try searching or replacing the following in your java String: \u00A0
.
这是一个不易破碎的空间。请参阅:我有一个带有\ u00a0的字符串,我需要用"替换它。 str_replace失败
That's a non-breakable space. See: I have a string with "\u00a0", and I need to replace it with "" str_replace fails
rank = rank.replaceAll("\u00A0", "");
应该有效。也许添加一个双 \\
而不是 \
。
should work. Maybe add a double \\
instead of the \
.
这篇关于Java String trim无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!