What is StringUtils.stripEnd in Java?
stripEnd() is a static method of the StringUtils class that is used to remove a set of characters from the end of the given string.
- The method returns
nullwhen the input string points to anullreference. - The method returns an empty string when the input string is empty.
- If the strip character points to a
nullreference, then whitespace is considered the strip character.
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;
Syntax
public static String stripEnd(final String str, final String stripChars)
Parameters
final String str: The string to remove characters from.final String stripChars: The characters to remove.
Return value
This method returns a new string with the strip characters stripped off from the end of the string.
Code
import org.apache.commons.lang3.StringUtils;public class Main {public static void main(String[] args) {String s = " 543234asfg ";String stripChars = " ";System.out.printf("The output of StringUtils.stripEnd() for the string - '%s' is '%s'", s, StringUtils.stripEnd(s, stripChars));System.out.println();s = " 543234asfg ";stripChars = null;System.out.printf("The output of StringUtils.stripEnd() for the string - '%s' is '%s'", s, StringUtils.stripEnd(s, stripChars));System.out.println();s = "";stripChars = "inda";System.out.printf("The output of StringUtils.stripEnd() for the string - '%s' is '%s'", s, StringUtils.stripEnd(s, stripChars));System.out.println();s = null;stripChars = "inda";System.out.printf("The output of StringUtils.stripEnd() for the string - '%s' is '%s'", s, StringUtils.stripEnd(s, stripChars));System.out.println();s = "hello-educative-hello";stripChars = "hello";System.out.printf("The output of StringUtils.stripEnd() for the string - '%s' is '%s'", s, StringUtils.stripEnd(s, stripChars));System.out.println();}}
Output
The output of the code will be as follows:
The output of StringUtils.stripEnd() for the string - ' 543234asfg ' is ' 543234asfg'
The output of StringUtils.stripEnd() for the string - ' 543234asfg ' is ' 543234asfg'
The output of StringUtils.stripEnd() for the string - '' is ''
The output of StringUtils.stripEnd() for the string - 'null' is 'null'
The output of StringUtils.stripEnd() for the string - 'hello-educative-hello' is 'hello-educative-'
Explanation
Example 1
- string -
" 543234asfg " - strip characters -
" "
The method returns " 543234asfg" with the whitespace stripped from the end of the string.
Example 2
- string -
" 543234asfg " - stripChars -
null
The method returns " 543234asfg" as the strip characters point to a null reference, and hence the whitespace is considered the strip character.
Example 3
- string -
"" - strip characters -
"inda"
The method returns '', as the input string is empty.
Example 4
- string -
null - strip characters -
"inda"
The method returns null, as the string points to a null reference.
Example 5
- string -
"hello-educative-hello" - stripChars -
"hello"
The method returns hello-educative-, with the strip characters stripped from the end of the string.