stripAll()
methodstripAll()
is a static method of the StringUtils
class that accepts a list of strings and removes characters from the beginning and end of every string in the given list of strings. The method returns null
when the input string points to a null
reference.
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[] stripAll(final String... strs)
The stripAll
method strips off whitespace from the beginning and end of every passed string.
Refer to What are the three dots in Java? to understand
...
.
final String... strs
: the list of strings to remove whitespace from.stripAll
returns an array that contains the strings with whitespace stripped off from the beginning and end of each string.
public static String[] stripAll(final String[] strs, final String stripChars)
The method strips off the strip characters passed to the method from the beginning and end of each string in the passed array of strings. If the strip character points to a null
reference, then whitespace is considered as the strip character.
final String[] strs
: an array of stringsfinal String stripChars
: the characters to removeThe stripAll
method returns a new array that contains the strings with the strip characters stripped off from the beginning and end of each string.
import org.apache.commons.lang3.StringUtils;import java.util.Arrays;public class Main {public static void main(String[] args) {String[] s = {" educative ", " educative", "educative "};System.out.printf("The output of StringUtils.stripAll() for the strings - '%s' is '%s'", Arrays.toString(s), Arrays.toString(StringUtils.stripAll(s)));System.out.println();s = new String[]{"hello-educative-hello", "educative-hello", "hello-educative"};String stripChars = "hello";System.out.printf("The output of StringUtils.stripAll() for the strings - '%s' is '%s'", Arrays.toString(s), Arrays.toString(StringUtils.stripAll(s, stripChars)));System.out.println();}}
The output of the code will be as follows.
The output of StringUtils.stripAll() for the strings - '[ educative , educative, educative ]' is '[educative, educative, educative]'
The output of StringUtils.stripAll() for the strings - '[hello-educative-hello, educative-hello, hello-educative]' is '[-educative-, ducative-, -educativ]'
string list - "[" educative ", " educative", "educative "]"
The method returns ["educative", "educative", "educative"]
, with the whitespace stripped from the start and end of each string.
string list - "["hello-educative-hello", "educative-hello", "hello-educative"]"
strip characters - "hello"
The method returns [-educative-, ducative-, -educativ]
, with the strip characters stripped from the start and end of each string.