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.
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
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.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
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.
Turn Learning into Career Growth
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.




