What is the StringUtils.stripStart() method in Java?

Overview

stripStart() is a static method of the StringUtils class that is used to remove a set of characters from the beginning of a string.

If any of these characters point to a null reference, the whitespace is considered the character to be removed.

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

The syntax of the stripStart() method is shown below:

public static String stripStart(final String str, final String stripChars)

Parameters

The stripStart() method accepts the following parameters:

  • final String str: the string to remove string characters from.
  • final String stripChars: the characters to remove.

Return value

This method returns a new string with the characters to remove stripped off from the start of the string.

  • The method returns null when the input string points to a null reference.
  • The method returns an empty string when the input string is empty.

Code

The code below shows how the stripStart() method works in Java:

import org.apache.commons.lang3.StringUtils;
public class Main {
public static void main(String[] args) {
// Example 1
String s = " 543234asfg ";
String stripChars = " ";
System.out.printf("The output of StringUtils.stripStart() for the string - '%s' is '%s'", s, StringUtils.stripStart(s, stripChars));
System.out.println();
// Example 2
s = " 543234asfg ";
stripChars = null;
System.out.printf("The output of StringUtils.stripStart() for the string - '%s' is '%s'", s, StringUtils.stripStart(s, stripChars));
System.out.println();
// Example 3
s = "";
stripChars = "inda";
System.out.printf("The output of StringUtils.stripStart() for the string - '%s' is '%s'", s, StringUtils.stripStart(s, stripChars));
System.out.println();
// Example 4
s = null;
stripChars = "inda";
System.out.printf("The output of StringUtils.stripStart() for the string - '%s' is '%s'", s, StringUtils.stripStart(s, stripChars));
System.out.println();
// Example 5
s = "hello-educative-hello";
stripChars = "hello";
System.out.printf("The output of StringUtils.stripStart() for the string - '%s' is '%s'", s, StringUtils.stripStart(s, stripChars));
System.out.println();
}
}

Output

The output of the code above will be as follows:

The output of StringUtils.stripStart() for the string - '  543234asfg   ' is '543234asfg   '
The output of StringUtils.stripStart() for the string - '  543234asfg   ' is '543234asfg   '
The output of StringUtils.stripStart() for the string - '' is ''
The output of StringUtils.stripStart() for the string - 'null' is 'null'
The output of StringUtils.stripStart() for the string - 'hello-educative-hello' is '-educative-hello'

Explanation

Example 1

  • string - " 543234asfg "
  • strip characters - " "

The method returns "543234asfg " with the whitespace stripped from the start of the string.

Example 2

  • string - ""
  • strip characters - "inda"

The method returns '', as the input string is empty.

Example 3

  • string - null
  • strip characters - "inda"

The method returns null, as the string points to a null reference.

Example 4

  • string - "hello-educative-hello"
  • strip characters - "hello"

The method returns -educative-hello, with the strip characters stripped from the start of the string.

Example 5

  • string - " 543234asfg "
  • strip characters - null

The method returns "543234asfg " as the strip characters point to a null reference, and hence the whitespace is considered the strip character.

Free Resources