Java String trim() method with examples

  • Method declaration – public String trim()
  • What does it do? trim() method will remove all leading and trailing spaces from the string, where space is defined as any character whose codepoint is less than or equal to ‘U+0020’ ( the space character).
  • What does it return? If returns a string with all of its leading and trailing spaces removed.

Some important points about the trim() method

  • It will be wrong to say that it removes all kinds of whitespaces because there are many types of whitespaces, and the whole list of them is available in this Wikipedia article. We will also show you this with an example later in the post.
  • Remember here that space is defined as any character whose codepoint is less than or equal to ‘U+0020’. So, if we say that the function removes leading or trailing whitespaces, it will remove all leading or trailing characters whose codepoint is less than or equal to ‘U+0020’.
  • If there are no leading and trailing whitespaces in the string, it will return the same string to the calling function.
  • And if there are leading or trailing whitespaces present in the string, then the function returns the substring of the string with all leading and trailing spaces removed, and it will be a new object reference as the string is immutable.
  • If only spaces are present in the string, then a String object representing an empty string will be returned.

Code example

We will be starting with a simple example to be familiar with the trim() method.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "   codekru   ";
		System.out.println("Before using trim fucntion: " + "====" + str1 + "=====");

		String str2 = str1.trim();
		System.out.println("After using trim fucntion: " + "====" + str2 + "=====");
	}
}

Output –

Before using trim fucntion: ====   codekru   =====
After using trim fucntion: ====codekru=====

You can see that leading and trailing spaces were present in the string before using the trim() method, and afterward, there were no.

The What If scenarios

What happens if we use different whitespace from the list we shared and not a normal ‘U + 0020’?

So, let’s use ‘U + 00A0’ whitespace and see how the trim method behaves.

public class Codekru {

	public static void main(String[] args) {

		String str1 = '\u2003' + "codekru";
		System.out.println("Before using trim fucntion: " + "====" + str1 + "=====");

		String str2 = str1.trim();
		System.out.println("After using trim fucntion: " + "====" + str2 + "=====");

	}
}

Output –

Before using trim fucntion: ==== codekru=====
After using trim fucntion: ==== codekru=====

You can see here that the whitespace is not removed. If you want to remove different whitespaces, we recommend using the String class’s strip() method.

What if we use the trim() method on a String containing spaces?

In this case, the method will return an empty string. Remember, the string returned is empty and not null.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "     ";
		System.out.println("Before using trim fucntion: " + "====" + str1 + "=====");

		String str2 = str1.trim();
		System.out.println("After using trim fucntion: " + "====" + str2 + "=====");

		System.out.println("is str2 empty? " + str2.isEmpty());

	}
}

Output –

Before using trim fucntion: ====     =====
After using trim fucntion: =========
is str2 empty? true
What if we try to use the trim() method on a null string?

Yes, you guessed it right. It will give us NullPointerException as shown using the below code.

public class Codekru {

	public static void main(String[] args) {

		String in = null;

		String str = in.trim();
		System.out.println("After using trim fucntion: " + "====" + str + "=====");

	}
}

Output –

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.trim()" because "in" is null
Does the trim() method return the same string reference if it doesn’t remove any whitespaces?

We know it’s not a what-if scenario, but we still want to put it there. And the answer to the question is Yes. Let’s see it with an example.

public class Codekru {

	public static void main(String[] args) {

		String str1 = "codekru";

		String str2 = str1.trim();

		if (str1 == str2) {
			System.out.println("str1 and str2 pointing to the same reference");
		} else {
			System.out.println("str1 and str2 are not pointing to the same reference");
		}
	}
}

Output –

str1 and str2 pointing to the same reference

If you want to know more about strings and their methods, we recommend reading this article.

We hope that you have liked the article. If you have any doubts or concerns, please feel free to write us in the comments or mail us at [email protected]

Liked the article? Share this on

Leave a Comment

Your email address will not be published.