What is StringUtils.removeIgnoreCase() in Java?

The removeIgnoreCase() method

removeIgnoreCase() 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 all occurrences of a substring from within the source string.

Different cases in removeIgnoreCase()

The comparison of the substring with the source string is case-insensitive in nature.

  • An empty source string will return the empty string.

  • An empty remove string will return the source string.

  • A null source string will return null.

  • A null remove 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 removeIgnoreCase(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-hello-educative";
String removalString = "eduCAT";
System.out.printf("StringUtils.removeIgnoreCase(%s, %s) = %s", sourceString, removalString, StringUtils.removeIgnoreCase(sourceString, removalString));
System.out.println();
// Example 2
sourceString = "hello-educative-hello-educative";
removalString = "oll";
System.out.printf("StringUtils.removeIgnoreCase(%s, %s) = %s", sourceString, removalString, StringUtils.removeIgnoreCase(sourceString, removalString));
System.out.println();
}
}

Example 1

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

The method returns hello-ive-hello-ive, removing all the occurrences of the removal string from the source string without case-considerations.

Example 2

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

The method returns hello-educative-hello-educative, i.e., the source string, as the removal string is not present in the source string.

Output

The output of the code will be as follows:


StringUtils.removeIgnoreCase(hello-educative-hello-educative, eduCAT) = hello-ive-hello-ive
StringUtils.removeIgnoreCase(hello-educative-hello-educative, oll) = hello-educative-hello-educative

Free Resources

Attributions:
  1. undefined by undefined
  2. undefined by undefined