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.
StringUtilsThe 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-langpackage, 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.
strip() methodThe first variation of the strip() method strips off whitespaces from the beginning and end of the string.
The syntax to remove whitespaces with the strip() method is as follows:
public static String strip(final String str)
final String str: the string to remove whitespaces from.The strip() method returns the string with the whitespaces stripped off from the beginning and end of the string.
strip() methodThe 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.
The syntax to remove characters with the strip() method is as follows:
public static String strip(String str, final String stripChars)
Final String str: the string to remove string characters from.Final String stripChars: the characters to remove.The strip() method returns a new string with stripChars stripped off from the beginning and end of the string.
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 1String s = " 543234asfg ";System.out.printf("The output of StringUtils.strip() for the string - '%s' is '%s'", s, StringUtils.strip(s));System.out.println();// Example 2s = "";System.out.printf("The output of StringUtils.strip() for the string - '%s' is '%s'", s, StringUtils.strip(s));System.out.println();// Example 3s = null;System.out.printf("The output of StringUtils.strip() for the string - '%s' is '%s'", s, StringUtils.strip(s));System.out.println();// Example 4s = "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 5s = " 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();}}
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'
" 543234asfg "The strip() method in line returns 543234asfg, with all the whitespaces stripped from the start and end of the string.
""The strip() method in line returns '', as the string is empty.
nullThe strip() method in line returns null, as the string points to a null reference.
"hello-educative-hello""hello"The strip() method in line returns -educative-, with the strip characters stripped from the start and end of the string.
"hello-educative-hello"nullThe strip() method in line returns hello-educative-hello, as the strip characters point to anull reference. Therefore, the whitespace is considered the strip character.