• Post Reply Bookmark Topic Watch Topic
  • New Topic

trim method on strings  RSS feed

 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, the trim method applied to string trims all the unnecessay spaces, like tabs, new lines and in general white spaces around a string (before or after).
So I tried this code:


Now, myString is an empty string and I would have thought that trim would remove that space and therefore the comparison would yield true but in fact it comes back with false. Why isn't trim removing that? I suppose it doesn't qualify as "white space around a string"?
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
myString is not an empty String, it has a single character which happens to be a space character. The value you assign to toCompare is an empty string, meaning it has zero characters. An empty string is never equal to a non-empty string.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you check myString1 and toCompare, you will find that they are equal
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no myString is not an empty string(even after trim applied).
answer-trim() returns a copy of the String i.e.,myString.trim() will produce a empty string which is referenced by myString1 but it will not cause myString to reference this copy.myString would still reference the same string which is-" ".though you can check for the equality of myString1 and toCompare that will return true.

Hope this will help!
Kind Regards,
Praveen.
 
praveen kumaar
Ranch Hand
Posts: 461
22
Android Chrome Eclipse IDE Google App Engine Java Notepad Oracle Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Edit]for line 2-trim returns copy of a string with trailing and whitespaces omitted.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:. . . would yield true but in fact it comes back with false. Why isn't trim removing that? I suppose it doesn't qualify as "white space around a string"?
You have forgotten that Strings are immutable. You think that applying the trim method to myString changes myString from " " to "", but it doesn't. It creates a new String which is equal to "" and that is assigned to myString1. If you compare myString1 and toCompare for equality you shou‍ld get true.
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right thanks guys, a few things that are not clear to me though.
If you check myString1 and toCompare, you will find that they are equal
Why is that? they contains different strings, one contains " " and one is an empty string. We said they are different and that trim() does not change " " to "".
Other thing is that this should return true shouldn't it? But it returns false?
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
to expand on what I said, sorry, yes I understand that trim doesn't affect the original string because strings are immutable, but I copied the result into a new string myString1, so does it mean that myString1 gets "" and not " "?
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:. . .  so does . . . myString1 [get] "" and not " "?
Yes.

Read the API documentation carefully, because String#trim() seems to me to use a rather strange definition of whitespace.
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, if I understand the definition correctly, because '\u0020' is the space character (and only up to it) when trim is used, then trim() on a " " character (the space character that is) returns "". That now explains perfectly why returns true. Cool providing my interpretation is correct, I'm satisfied now :-)!
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Right, if I understand the definition correctly, because '\u0020' is the space character (and only up to it) when trim is used, then trim() on a " " character (the space character that is) returns "". That now explains perfectly why returns true. Cool providing my interpretation is correct, I'm satisfied now :-)!


That statement prints true because a string object is always equal to itself.

Henry
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Right, if I understand the definition correctly, because '\u0020' is the space character (and only up to it) when trim is used, then trim() on a " " character (the space character that is) returns "". . . .
Exactly. But:


If you go to the Pattern class documentation and scroll down, you will find a definition of whitespace
\s     A whitespace character: [ \t\n\x0B\f\r]
Look up 0b here, and you will find it is VT (=vertical tab). Now, if you investigate those characters (link on the left), you find that they all fulfil the property of being ≤ 0x0020. So far, so good. But that pattern matches 6 different characters and there are 32 which are ≤ 0x0020, if we exclude the null character. So String#trim counts 26 other characters as whitespace. Not that you are likely ever to use things like (char)7 (old BELL character) in yout Strings. But String#trim would see that as whitespace. If you go back to Scanner and look up and down a line or two, you find horizontal and vertical whitespace definitions, which include about twenty characters which are > 0x0020. If you include them in your Strings (and you probably won't) String#trim will not recognise them as whitespace.
As long as you stick to well‑known characters, you won't have any problems with those definitions.
 
Jason Attin
Ranch Hand
Posts: 234
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That statement prints true because a string object is always equal to itself.

Eh, darn copy and paste...lol! Sorry I meant the statement
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!