Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is StringUtils.removeStartIgnoreCase() 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.

removeStartIgnoreCase() is a staticthe methods in Java that can be called without creating an object of the class. method of the StringUtils class that is used to remove a substring only if it is at the beginning of a source string. Otherwise, it returns the source string. The matching is case-insensitive in nature.

  • A null source string will return null.
  • An empty ("") source string will return the empty string.
  • A null search string will return the source string.

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 removeStartIgnoreCase(final String str, final String remove)

Parameters

  • final String str: The source string to search.
  • final String remove: The string to search and remove.

Return value

This method returns the substring with the string removed, if found.

Code

import org.apache.commons.lang3.StringUtils;
public class Main{
public static void main( String args[] ) {
// Example 1
String sourceString = "hello-educative";
String removalString = "HElLo";
System.out.printf("StringUtils.removeStartIgnoreCase(%s, %s) = %s", sourceString, removalString, StringUtils.removeStartIgnoreCase(sourceString, removalString));
System.out.println();
// Example 2
sourceString = "hello-educative";
removalString = "educat";
System.out.printf("StringUtils.removeStartIgnoreCase(%s, %s) = %s", sourceString, removalString, StringUtils.removeStartIgnoreCase(sourceString, removalString));
System.out.println();
}
}

Output

The output of the code will be as follows:


StringUtils.removeStartIgnoreCase(hello-educative, HElLo) = -educative
StringUtils.removeStartIgnoreCase(hello-educative, educat) = hello-educative

Explanation

Example 1

  • Source String = "hello-educative"
  • Removal String = "HElLo"

The method returns -educative, as the source string starts with the removal string ignoring the case considerations, and the removal string is removed from the source string.

Example 2

  • Source String = "hello-educative"
  • Removal String = "educat"

The method returns hello-educative, as the source string does not start with the removal string and the source string is returned as-is.

RELATED TAGS

java
communitycreator

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