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.
null
when the input string points to a null
reference.null
reference, then whitespace is considered the strip character.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;
public static String stripEnd(final String str, final String stripChars)
final String str
: The string to remove characters from.final String stripChars
: The characters to remove.This method returns a new string with the strip characters stripped off from the end of the string.
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();}}
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-'
" 543234asfg "
" "
The method returns " 543234asfg"
with the whitespace stripped from the end of the string.
" 543234asfg "
null
The method returns " 543234asfg"
as the strip characters point to a null
reference, and hence the whitespace is considered the strip character.
""
"inda"
The method returns ''
, as the input string is empty.
null
"inda"
The method returns null
, as the string points to a null
reference.
"hello-educative-hello"
"hello"
The method returns hello-educative-
, with the strip characters stripped from the end of the string.