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.
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;
The syntax of the stripStart()
method is shown below:
public static String stripStart(final String str, final String stripChars)
The stripStart()
method accepts the following parameters:
final String str
: the string to remove string characters from.final String stripChars
: the characters to remove.This method returns a new string with the characters to remove stripped off from the start of the string.
null
when the input string points to a null
reference.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 1String 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 2s = " 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 3s = "";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 4s = 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 5s = "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();}}
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'
" 543234asfg "
" "
The method returns "543234asfg "
with the whitespace stripped from the start of the string.
""
"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 -educative-hello
, with the strip characters stripped from the start 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.