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.
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;
public static boolean containsAnyIgnoreCase(CharSequence cs, CharSequence... searchCharSequences)
The function takes in two parameters:
CharSequence cs
: Character sequence to search in.searchCharSequences
: The array of character sequences to search for.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.
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));}}
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
.
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
.
The output of the code will be as follows:
false
true