String.trim() bug...

In this case String.trim() returns "hello "

The character it has trouble trimming is known in HTML as& n b p s ;

Is this a bug? it certainly looks like white space if you ask me.

publicclass NoBreakTrimTest{

publicstaticvoid main(String[] args){

char c = 0x00A0;

String input ="hello" + c;

String trimmed = input.trim();

System.out.println("Trimmed: " + trimmed);

boolean equal = trimmed.equals("hello");

System.out.println("Equal: " + equal);

}

}

[981 byte] By [jvaudrya] at [2007-11-27 8:41:01]
# 1
It's as designed.
jverda at 2007-7-12 20:39:46 > top of Java-index,Java Essentials,Java Programming...
# 2
Ah yes! The old "Java doesn't do what I want it to do, so it must be a bug" school of thought.
floundera at 2007-7-12 20:39:46 > top of Java-index,Java Essentials,Java Programming...
# 3

String.trim() only removes all spaces before and after any characters in the string.For example:

[b]Input:[/b]"nbsp;"

[b]Output:[/b]"nbsp;"

But

[b]Input:[/b]"n b s p ;"

[b]Output:[/b]"n b s p ;"

I.E , its not a bug , you have misunderstood what String.trim() does

Overkilla at 2007-7-12 20:39:46 > top of Java-index,Java Essentials,Java Programming...
# 4
Yeh, it's a bug. File a bug report
georgemca at 2007-7-12 20:39:46 > top of Java-index,Java Essentials,Java Programming...
# 5
Err that was a joke ;-)OP, if it's a bug, please show us what part of the specification of String.trim() is being violated.
ejpa at 2007-7-12 20:39:46 > top of Java-index,Java Essentials,Java Programming...
# 6

> Err that was a joke ;-)

Not completely, no. This sort of thread has a tendency to degenerate quite quickly into a flamewar when the OP won't accept that it isn't a bug. Seems easier to just let them file a bug report and get on with other things!

> OP, if it's a bug, please show us what part of the

> specification of String.trim() is being violated.

I'd add to that, "be prepared to reverse your belief in this bug, if it is shown not to be a bug".

georgemca at 2007-7-12 20:39:46 > top of Java-index,Java Essentials,Java Programming...
# 7

Hi,

I was not thinking it was a bug as in a bug Sun would fix:

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4080617

Rather, I thought, this is just really weird

input.trim() // returns "hello "

It seems like trim() only works on ASCII strings. Too bad for all those suckers who need to trim Unicode strings!

jvaudrya at 2007-7-12 20:39:46 > top of Java-index,Java Essentials,Java Programming...
# 8

According to the spec: "If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned."

So, it seems that it will work with Unicode strings, but no-break spaces are not considered to be in the scope of whitespace characters.

RATiXa at 2007-7-12 20:39:46 > top of Java-index,Java Essentials,Java Programming...
# 9

> String.trim() only removes all spaces before and

> after any characters in the string.For example:

Actually, as others have pointed out, it removes "white space" characters not just spaces

> I.E , its not a bug , you have misunderstood what

> String.trim() does

Ditto.

jbisha at 2007-7-12 20:39:47 > top of Java-index,Java Essentials,Java Programming...