Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
stringutils
communitycreator

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

containsAnyIgnoreCase is a static method of the StringUtils class that checks whether a given string contains any of the characters/strings in the given set of characters/strings while ignoring the case considerations.

At least one search character/string should be present in the string, ignoring the case, for this function to return true.


The comparison is case-insensitive in nature.

How to import StringUtils

StringUtils is defined in the Apache Commons Lang package. Apache Commons Lang can be added 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>

Note: 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 boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences) 

Parameters

The function takes in two parameters:

  • CharSequence cs: Character sequence to search in.
  • searchCharSequences: The array of character sequences to search for.

Return value

The function returns true if the character sequence contains any of the search character sequences ignoring the case. Otherwise, it returns false.


The function will return false in case of a null parameter.

Code

import org.apache.commons.lang3.StringUtils;
public class Main{
public static void main(String[] args){
String characterSequence = "ABBsscDDDlssfffddFe";
String searchString = "ewq";
String searchString2 = "eF";
System.out.println(StringUtils.containsAnyIgnoreCase(characterSequence, searchString, searchString2));
searchString = "Ab";
System.out.println(StringUtils.containsAnyIgnoreCase(characterSequence, searchString));
}
}

Explanation

  1. string = "ABBsscDDDlssfffddFe"
    search sequences = ["ewq", "eF"]

    Since the string does not contain at least one search sequence from the list of search sequences, the function would return false.

  2. string = "ABBsscDDDlssfffddFe"
    search sequences = ["Ab"]

    Since the string contains at least one search sequence from the list of search sequences, the function would return true.

Output

The output of the code will be as follows:


false
true

RELATED TAGS

java
stringutils
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