Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java

What is StringUtils.stripAll in Java?

Abhilash

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The stripAll() method

stripAll() 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.

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[] 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 ....

Parameters

  • final String... strs: the list of strings to remove whitespace from.

Return value

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.

Parameters

  • final String[] strs: an array of strings
  • final String stripChars: the characters to remove

Return value

The stripAll method returns a new array that contains the strings with the strip characters stripped off from the beginning and end of each string.

Code

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();
}
}

Output

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]'

Explanation

Example 1

  • string list - "[" educative ", " educative", "educative "]"

The method returns ["educative", "educative", "educative"], with the whitespace stripped from the start and end of each string.

Example 2

  • 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.

RELATED TAGS

java

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring