
- Java.lang - Home
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang Package Useful Resources
- Java.lang - Useful Resources
- Java.lang - Discussion
Java - String trim() Method
Description
The Java String trim() method is used to remove the whitespaces from the beginning and the end of a string. It returns a copy of the string with leading and trailing spaces omitted or, if it has no leading or trailing whitespaces, this method returns the current string.
This method does not change the String object's value. We only need to assign the new String object to a new variable or reassign it to the original String to have access to it.
Note − Keep in mind that the middle spaces are not eliminated by the trim() method.
Syntax
Following is the syntax for Java String trim() method −
public String trim()
Parameters
This method does not accept any parameter.
Return Value
This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
Trimming Leading and Trailing Whitespaces from a String Example
The following example shows the usage of Java String trim() method by removing the leading and trailing white spaces from the given string " This is TutorialsPoint " −
package com.tutorialspoint; public class StringDemo { public static void main(String[] args) { // string with leading and trailing white space String str = " This is TutorialsPoint "; System.out.print("Before trim = "); System.out.println(".." + str + ".."); // leading and trailing white space removed System.out.print("After trim = "); System.out.println(".." + str.trim() + ".."); } }
Output
If you compile and run the program above, the output will be displayed as follows −
Before trim = .. This is TutorialsPoint .. After trim = ..This is TutorialsPoint..
Verifying Length after Trimming Leading and Trailing Whitespaces from a String Example
Below is another example to remove all the leading and trailing spaces using trim() method in Java. Here, we verifying the length of the string after the removal −
package com.tutorialspoint; import java.util.Locale; public class StringDemo { public static void main(String[] args) { String str = " Tutorials Point "; System.out.println("The length of the string before trimming is: " + str.length()); System.out.println("The string without trimming is: " + str); String t = str.trim(); System.out.println("The length of the string after trimming is: " + t.length()); System.out.println("The string with trimming is:" + t); } }
Output
If you compile and run the above program, it will produce the following result −
The length of the string before trimming is: 25 The string without trimming is: Tutorials Point The length of the string after trimming is: 15 The string with trimming is:Tutorials Point
Checking White Spaces after Trimming Leading and Trailing Whitespaces from a String Example
Let's create another code to check whether the given string contains only white spaces or not, using the if-else condition −
package com.tutorialspoint; public class StringDemo { public static void main(String argvs[]) { String s = " Tutorials Point "; if ((s.trim()).length() > 0) { System.out.println("The string consists of characters other than the white spaces \n"); } else { System.out.println("The string consists of only white spaces \n"); } s = " "; if ((s.trim()).length() > 0) { System.out.println("The string consists of characters other than the white spaces \n"); } else { System.out.println("The string consists of only white spaces \n"); } } }
Output
On executing the program above, the output is obtained as follows −
The string consists of characters other than the white spaces The string consists of only white spaces
Checking HashCode after Trimming Leading and Trailing Whitespaces from a String Example
Since strings in Java are immutable, while removing the white spaces from a string using the trim() method, a new string is returned. The reference to the same string is returned if the manipulation is not carried out by the trim() method as shown in the following example −
package com.tutorialspoint; public class StringDemo { public static void main(String argvs[]) { String s = " Tutorials Point "; String s1 = s.trim(); // the hashcode of s and s1 is different System.out.println("The hashcode of string s is: " + s.hashCode()); System.out.println("The hashcode of string s1 is: " + s1.hashCode()); String x = "calm"; String y = x.trim(); // the hashcode of x and y is the same System.out.println("The hashcode of string x is: " + x.hashCode()); System.out.println("The hashcode of string y is: " + y.hashCode()); } }
Output
The output of the above code is as shown below −
The hashcode of string s is: 932888837 The hashcode of string s1 is: -1622622651 The hashcode of string x is: 3045983 The hashcode of string y is: 3045983