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
  • 39,719
  • 12
  • 74
  • 101
Rais Alam
  • 6,892
  • 12
  • 51
  • 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
  • 203,151
  • 43
  • 392
  • 509
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
  • 2,945
  • 2
  • 26
  • 52
4

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

Arpit
  • 12,709
  • 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,040
  • 4
  • 23
  • 33
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
  • 24,753
  • 5
  • 47
  • 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,090
  • 6
  • 52
  • 80