Java - String trim() Method



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.

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 " −

import java.lang.*; 
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..

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 −

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

Example

Let's create another code to check whether the given string contains only white spaces or not, using the if-else condition −

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

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 −

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
java_lang_string.htm
Advertisements