Java String strip() Method



The Java String strip() method is used to remove the white spaces from the beginning and the end of the given string. It retrieves a copy of the string with leading and trailing spaces omitted or the same string is retrieved if it has no leading or trailing white space. This method produces the same result as the String trim() method.

The main difference between the trim() and strip() method is that the trim() method eliminates any spaces with a code point that is less than or equal to "U + 0020." Therefore, it won't be able to eliminate all kind of whitespaces. Whereas the strip() method can remove various whitespaces.

If the given string contains only white spaces, then this method results in an empty string.

Syntax

Following is the syntax for Java String strip() method −

public String strip()

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 strip() method. Here we are removing the leading and trailing white spaces from the 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 strip = ");
      System.out.println(".." + str + "..");
      
      // leading and trailing white space removed
      System.out.print("After strip = ");
      System.out.println(".." + str.strip() + "..");
   }
}

Output

If you compile and run the above program, it will produce the following result −

Before strip = .. This is TutorialsPoint ..
After strip = ..This is TutorialsPoint..

Example

Below is an example to remove all the leading and trailing spaces using strip() method in Java. Therefore, the length of the given string also reduces −

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 stripping is: " + str.length());
      System.out.println("The string without stripping is: " + str);
      String t = str.strip();
      System.out.println("The length of the string after stripping is: " + t.length());
      System.out.println("The string with stripping is:" + t);
   }
}

Output

If you compile and run the program above, the output will be displayed as follows −

The length of the string before stripping is: 25
The string without stripping is:     Tutorials Point
The length of the string after stripping is: 15
The string with stripping is:Tutorials Point

Example

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

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "  Tutorials Point  ";
      if ((s.strip()).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.strip()).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

As Java strings are immutable, when the white space is removed from a string using the strip() method, a new string is returned. And the reference to the same string is returned if the manipulation is not carried out successfully.

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "  Tutorials Point  ";
      String s1 = s.strip();
      
      // 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.strip();
      
      // 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 program is as follows −

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