What is StringUtils.strip() in Java?

Overview

strip() is a static method of the StringUtils class that is used to remove characters from the beginning and end of the given string. The method returns null when the input string points to a null reference.

How to import StringUtils

The definition of StringUtils can be found in the Apache Commons Lang package, which we can add to the Maven project by adding the following dependency to the pom.xml file.

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the StringUtils class as follows:


import org.apache.commons.lang3.StringUtils;

There are two variations of the strip() method.

Remove whitespaces with the strip() method

The first variation of the strip() method strips off whitespaces from the beginning and end of the string.

Syntax

The syntax to remove whitespaces with the strip() method is as follows:

public static String strip(final String str)

Parameters

  • final String str: the string to remove whitespaces from.

Return value

The strip() method returns the string with the whitespaces stripped off from the beginning and end of the string.

Remove characters with the strip() method

The second variation of the strip() method strips off the specified characters passed to the method from the beginning and end of the string. If the character to remove points to a null reference, then whitespace is considered as the character to remove.

Syntax

The syntax to remove characters with the strip() method is as follows:


public static String strip(String str, final String stripChars)

Parameters

  • Final String str: the string to remove string characters from.
  • Final String stripChars: the characters to remove.

Return value

The strip() method returns a new string with stripChars stripped off from the beginning and end of the string.

Code

The code below shows how the strip() method works in Java:

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
// Example 1
String s = " 543234asfg ";
System.out.printf("The output of StringUtils.strip() for the string - '%s' is '%s'", s, StringUtils.strip(s));
System.out.println();
// Example 2
s = "";
System.out.printf("The output of StringUtils.strip() for the string - '%s' is '%s'", s, StringUtils.strip(s));
System.out.println();
// Example 3
s = null;
System.out.printf("The output of StringUtils.strip() for the string - '%s' is '%s'", s, StringUtils.strip(s));
System.out.println();
// Example 4
s = "hello-educative-hello";
System.out.printf("The output of StringUtils.strip() for the string - '%s' is '%s'", s, StringUtils.strip(s, "hello"));
System.out.println();
// Example 5
s = " hello-educative-hello ";
System.out.printf("The output of StringUtils.strip() for the string - '%s' is '%s'", s, StringUtils.strip(s, null));
System.out.println();
}
}

Output

The output of the code will be as follows:

The output of StringUtils.strip() for the string - '  543234asfg   ' is '543234asfg'
The output of StringUtils.strip() for the string - '' is ''
The output of StringUtils.strip() for the string - 'null' is 'null'
The output of StringUtils.strip() for the string - 'hello-educative-hello' is '-educative-'
The output of StringUtils.strip() for the string - '  hello-educative-hello   ' is 'hello-educative-hello'

Explanation

Example 1

  • string - " 543234asfg "

The strip() method in line 1010 returns 543234asfg, with all the whitespaces stripped from the start and end of the string.

Example 2

  • string - ""

The strip() method in line 1515 returns '', as the string is empty.

Example 3

  • string - null

The strip() method in line 2020 returns null, as the string points to a null reference.

Example 4

  • string - "hello-educative-hello"
  • stripChars - "hello"

The strip() method in line 2525 returns -educative-, with the strip characters stripped from the start and end of the string.

Example 5

  • string - "hello-educative-hello"
  • stripChars - null

The strip() method in line 3030 returns hello-educative-hello, as the strip characters point to anull reference. Therefore, the whitespace is considered the strip character.

Free Resources