Java String trim() Method
Overview
The trim() in Java eliminates all leading and trailing whitespaces in a string. It is defined under the String class of the Java.lang package.
Syntax of trim() in Java
Following is the syntax of trim in Java:
Parameters of trim() in Java
The trim() in Java does not accept any parameter.
Return Value of trim() in Java
Return Type: String
The trim() method in Java returns a new string, which is the copy of the original string with leading and trailing spaces removed from it.
Example for trim() in Java
Suppose we are given the string: " Java " with whitespaces present at the beginning and at the end. To remove spaces, we can use the trim() method; the code given below illustrates this.
Code:
Output:
How Does the trim() Method Work in Java?
The Unicode value for the space character is ‘\u0020’. When the code is being processed in Java, the trim in Java checks whether the Unicode value ‘\u0020’ is present before and after the given string. If the Unicode value ‘\u0020’ is present, then the trim in Java eliminates the spaces and returns a new string.
Note:
- The trim() in Java returns a new String object with no leading and trailing spaces.
- The original string is not modified by this method.
- The trim() in Java does not remove the spaces in the middle of the string.
trim() vs replaceAll()
The trim() method only removes leading and trailing whitespaces, whereas the replaceAll() method removes all the whitespaces present in the string(including leading and trailing).
Now, let's understand the difference between the two with the help of examples.
Example 1: Java String trim()
Output:
Explanation of the Example:
In the above example, we are given a string " This is an example ". The given string has 1 leading space and 4 trailing spaces. Upon compiling the code, the trim in Java would encounter 1 ‘\u0020’ characters at the beginning and 4 ‘\u0020’ characters at the end. Thus, it would eliminate all these characters and return a new string that would output "This is an example".
Example 2: Remove All Whitespace Characters
In the above sections, we have learned how to remove leading and trailing spaces in a string. But what if we remove all the whitespaces in the string?
We can use the replaceAll() method in Java to remove all the whitespace characters from a string.
The following example shows how we can remove all whitespace characters from a string:
Output:
Explanation of the Example:
In the above example, we are given the string " This is an example " and we are supposed to remove all whitespace characters. To achieve this we are using str1.replaceAll() method.
Our first parameter is a regex notion for whitespace characters, and our second parameter is an empty string. Thus, the replaceAll() method will replace all the whitespace characters present in the string with an empty string, and we will get a new string with "Thisisanexample" as a result.
Conclusion
- trim() in Java removes all the leading and trailing spaces in the string.
- It does not take any parameter and returns a new string.
- The Unicode value for the space character is ‘\u0020’.
- The trim method checks for the Unicode value of the space and eliminates it.
- The trim() in Java does not remove middle spaces.
- If we want to remove all whitespace characters from a string, we must use the replaceAll() method.