Java - String stripIndent() Method



The Java String stripIndent() method is used to remove the incidental whitespaces from the beginning and the end of the given string line. This method does not remove the white space present in the middle of the string. The incidental white space is considered as the trailing white space on each line in a text block.

Syntax

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

public String stripIndent()

Parameters

This method does not accept any parameter.

Return Value

This method returns a copy of this string with incidental whitespace removed from the beginning and the end of the given string line.

Example

The following example shows the usage of Java String stripIndent() 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.println("The given string is: " + str + "..");
      
      // trailing white space removed
      System.out.println("The string after removing whitespace is: " + str.stripIndent() + "..");
   }
}

Output

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

The given string is:      This is TutorialsPoint     ..
The string after removing whitespace is: This is TutorialsPoint..

Example

Below is another example to remove all the leading and trailing spaces using stripIndent() method in Java. Here, you can observe that the length of these strings is reduced −

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.stripIndent();
      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: 19
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 the if-else condition −

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

When the trailing whitespace is removed from a string using the stripIndent() method, a new string is returned since strings in Java are immutable. The reference to the same string is returned if the manipulation is not carried out by the stripIndent() method as shown in the following example −

public class StringDemo {
   public static void main(String argvs[]) {
      String s = "Tutorials Point   ";
      System.out.println("The given string is: " + s + "..");
      String s1 = s.stripIndent();
      System.out.println("The string after removing trailing whitespace is: " + s1 + "..");
      
      // 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.stripIndent();
      
      // 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 given string is: Tutorials Point   ..
The string after removing trailing whitespace is: Tutorials Point..
The hashcode of string s is: 305552315
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