Java String Trim

We always love working with Strings in our own programs. Strings is one of the most common data structure in most programming language. One common thing we wish to do with String is to clean it up. One common way of cleaning up Strings is by removing spaces from the beginning and the end of the value. For example, when we want to save first name or last name, having spaces at the beginning or the end of a name doesn't make sense. So we want to remove them. This is where the Java String's Trim() method comes in handy. This is what it exactly do. Below are more discussion about how to use Trim method of Java String.

Syntax

Before we start with discussion, let's look at the syntax of the trim method of String class. We get below definition about Java String Trim method:

public String trim()

Note that this is not a static method, meaning it needs an instance to call it. Also note that it does not have any input argument either. It is the instance of the class where the method will be performed on. Take note also that this method will return an instance of a String with trimmed values.

Operation Explanation

So what does the trim method really do? Imagine that we have a String with both spaces in front and in the end as shown below:

String myString = "   Hello  ";

We could see that there are 3 spaces before the letter H and 2 spaces after the letter o. If we try to output this using below example:
String myString = "   Hello  ";
System.out.println(">" + myString +"<");
We append special character before and after the String so we could highlight the spaces. We will get below output"
>   Hello  <
And we still have spaces. What the Java String trim() method do is remove the spaces at the beginning and at the end. For example:
String myString = "   Hello  ";
System.out.println(">" + myString.trim() +"<");
The output will is shown below where all the spaces at the beginning ang the end are gone. But what about spaces in the middle? Below is an example:
>Hello<
String myString = "   Hello World  ";
System.out.println(">" + myString.trim() +"<");
The spaces in the middle is not removed. Because trim only remove spaces at both end. Below is the output:
>Hello World<
If we only have spaces at the front as shown below:
String myString = "   Hello World";
System.out.println(">" + myString.trim() +"<");
Will also yield
>Hello World<
And the inverse, which is having spaces at the end of the String, for example:
String myString = "Hello World   ";
System.out.println(">" + myString.trim() +"<");
Will still give us the same results:
>Hello World<

Trim String with no Spaces

If the instance of String where we called trim() method from does not contain spaces at the beginning and at the end, as shown below:
String myString = "Test";
System.out.println(">" + myString.trim() +"<");
Then the trim method will just return the original String value, hence the expected output below:
>Test<

Trim Empty String

Similar to the example above, if we try to trim a String that is empty, meaning the length is 0, like shown below:
String myString = "";
System.out.println(">" + myString.trim() +"<");

It will just result to also an empty String, as shown in the output below:

><

String trim() Pitfall

Please be very careful that since String is an immutable object, the trim method does not mutate the String itself, but creates a new String and returns it to the caller. What do we mean by this? See this example:
String myString = "   Hello World   ";
myString.trim();
System.out.println(">" + myString +"<");
The output is shown below which could surprise anyone because the spaces were not removed.
>   Hello World   <
Why is this so? It's because myString was not mutated by the call of the method trim(). What we get is that a new copy of String was created with the trimmed values, and returned to the caller, but leaving the instance in the same state. So what do we do if we want the new String with trimmed values? We can assign it back to the variable itself as shown below:
String myString = "   Hello World   ";
myString = myString.trim();
System.out.println(">" + myString +"<");

And now myString gets the trimmed version of the original String because we assigned it back to the same variable. The output is shown below.

>Hello World<

We could also assign it to another variable if we want to, for example:
String myString = "   Hello World   ";
String newString = myString.trim();
System.out.println(">" + newString +"<");

Which will bear the same output as shown below:

>Hello World<

Yet another example to stress the point:
String myString = "   Hello World   ";
String newString = myString.trim();
System.out.println(">" + myString +"<");
System.out.println(">" + newString +"<");
We will get the following results:
>   Hello World   <
>Hello World<
Why? Because the first String was not assigned a new value. So even calling the trim() method out of this String in Java, will not result into a new state. The variable myString will still contain the original value. But since we assigned the result of the trim() method of the original String to the variable newString, it is now an instance holding the mutated value with no spaces at the beginning and at the end.