Java String strip() Method

Java String strip() Method | The strip() method of the Java String class is used to remove leading and trailing spaces in the string. It can remove all unwanted spaces. The three variations of this method are shown below:-

  1. public String stripLeading():- removes the white spaces at the start of the string.
  2. public String stripTrailing():- removes the white spaces at the end of the string.
  3. public String strip():- removes the white spaces at both the start and end of the string. But it won’t remove any white space between the words.

Below are the examples for all three variations of the String Strip Java:-
1. String = ” Hello people good evening “
After using the strip method = “Hello people good evening”. It will remove both leading and trailing white spaces.

2. String = ” Hey this is Java programming language “
After using stripLeading() = “Hey this is Java programming language “. It will remove only leading white spaces.

3. String = ” Strip method has three variants “
After using stripTrailing() = ” Strip method has three variants”. It will remove only trailing white spaces.

Java String strip() Method Example

Now let us see an example of the Java string class strip() method to understand the working of the strip() method.

public class Main {
   public static void main(String[] args) {
      String string = "   Know Program   ";
      System.out.println("Input string length: " + string.length());
      String str = string.strip();
      System.out.println("Resultant string: \"" + str + "\"");
      System.out.println("Stripped string length: " + str.length());
   }
}

Output:-

Input string length: 18
Resultant string: “Know Program”
Stripped string length: 12

Notice that strip() method removed both leading and trailing white spaces but it doesn’t remove the spaces between the words.

Java String stripLeading() Method Example

This example shows the working of the leading strip. 

public class Main {
   public static void main(String[] args) {
      String string = "   Know Program   ";
      System.out.println("Input string length: " + string.length());
      String str = string.stripLeading();
      System.out.println("Resultant string: \"" + str + "\"");
      System.out.println("Stripped string length: " + str.length());
   }
}

Output:-

Input string length: 18
Resultant string: “Know Program “
Stripped string length: 15

Java String stripTrailing() Method Example

This method shows the working of the trailing method.

public class Main {
   public static void main(String[] args) {
      String string = "   Know Program   ";
      System.out.println("Input string length: " + string.length());
      String str = string.stripTrailing();
      System.out.println("Resultant string: \"" + str + "\"");
      System.out.println("Stripped string length: " + str.length());
   }
}

Output:-

Input string length: 18
Resultant string: ” Know Program”
Stripped string length: 15

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *