removeIgnoreCase()
methodremoveIgnoreCase()
is a StringUtils
class that is used to remove all occurrences of a substring from within the source string.
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.
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;
public static String removeIgnoreCase(final String str, final String remove)
final String str
: The source string to search.final String remove
: The string to search and remove.This method returns the substring with the string removed, if found.
import org.apache.commons.lang3.StringUtils;public class Main{public static void main( String args[] ) {// Example 1String 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 2sourceString = "hello-educative-hello-educative";removalString = "oll";System.out.printf("StringUtils.removeIgnoreCase(%s, %s) = %s", sourceString, removalString, StringUtils.removeIgnoreCase(sourceString, removalString));System.out.println();}}
"hello-educative-hello-educative"
"eduCAT"
The method returns hello-ive-hello-ive
, removing all the occurrences of the removal string from the source string without case-considerations.
"hello-educative-hello-educative"
"oll"
The method returns hello-educative-hello-educative
, i.e., the source string, as the removal string is not present in the source string.
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