3

I learned string.trim() removes leading and trailing spaces. But in my case its not working i am trying below code but output is with leading and trailing space. But my expectation is text without leading and trailing space. Here is my code.

String s = " Hello Rais ";
s += " Welcome to my World ";
s.trim( );
System.out.println(s);

Please help me

Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
Rais Alam
  • 6,970
  • 12
  • 53
  • 84
  • 1
    [The API in this case is very helpful.](http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#trim()) You *do* have to reassign it to the String you want - Strings are immutable. – Makoto Feb 17 '13 at 07:48
  • 3
    @Real - repeat after me. "Java Strings are immutable". :-) – Stephen C Feb 17 '13 at 07:48
  • @StephenC My code is working i was making a silli mistake. I have to assingned the return of trim function to the variable. I dont understand What are you trying to say in above comment. – Rais Alam Feb 17 '13 at 07:53
  • What did you see vs. What did you expect to see? – George Stocker Nov 18 '13 at 18:47

6 Answers6

10

You need to re-assign the result of trim back to s:

s = s.trim();

Remember, Strings in Java are immutable, so almost all the String class methods will create and return a new string, rather than modifying the string in place.


Although this is off-topic, but (as I said there almost), it's worth knowing that, exception to this rule is when creating a substring of same length, or any time a method returns the string with the same value, which will be optimized and will not create a new string, but simply return this.

String s = "Rohit";
String s2 = s.substring(0, s.length());

System.out.println(s == s2); // will print true
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • +1 `System.out.println(s == s2);` will be true because s and s2 has same value so for optimization they will hold same place in string pool managed by jvm. http://stackoverflow.com/questions/3801343/what-is-string-pool-in-java – Tapas Bose Feb 17 '13 at 07:57
  • @Rohit - thanks it was my mistake i forgot to re assign – Rais Alam Feb 17 '13 at 07:57
  • @TapasBose. Yeah right. Should have mentioned that point. Thanks :) – Rohit Jain Feb 17 '13 at 08:01
  • You shoud use System.out.println(s.eqauals(s2)) to check the equality. – MayurB Feb 17 '13 at 08:08
  • @MayurB. Is that for me? Thanks but I know that. The code is just an illustration of exception where `==` returns `true`. – Rohit Jain Feb 17 '13 at 08:09
6

In Java Strings are immutable. So s.trim() does not modify the original string but returns a new string.

String trimmed  = s.trim();
MoveFast
  • 3,011
  • 2
  • 27
  • 53
4

just add s=s.trim( ); because trim returns a new string.

Arpit
  • 12,767
  • 3
  • 27
  • 40
3

Well..string is immutable object. so whenever you do trim(), it creates a brand new String object, which need to have a reference to access it. So do assign a reference to this trimmed String object as follows.

s = s.trim();

or

trimmedS = s.trim();
Ahmad
  • 2,110
  • 5
  • 26
  • 36
3

Understand that String in Java is immutable. Which means any operation on the String class does not change the internal string itself, but returns a new String object.

So you really need to do

s = s.trim()

which assigns the reference s to a new String object that has its trailing and leading spaces removed.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
2

trim function returns a copy of the original string by trimming the white spaces so you need to store the newly returned string like s = s.trim()

From the javadocs of String#trim()

trim

public String trim()
Returns a copy of the string, with leading and trailing whitespace omitted.
Abubakkar
  • 15,488
  • 8
  • 55
  • 83