What is StringUtils.containsIgnoreCase in Java?
What is containsIgnoreCase?
containsIgnoreCase is a static method of the StringUtils class that checks whether a given string contains the complete search string as a whole, while ignoring the case considerations.
The comparison is case-insensitive in nature. Refer to What is StringUtils.contains in Java? for case-sensitive comparison.
Add Apache Common Langs package
StringUtils is defined in the Apache Commons Lang package. To add Apache Commons Lang to the Maven project, add 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.
How to import StringUtils
You can import the StringUtils class as follows.
import org.apache.commons.lang3.StringUtils;
Syntax
public static boolean containsIgnoreCase(final CharSequence str, final CharSequence searchStr)
Parameters
-
final CharSequence str: the character sequence to search in. -
final CharSequence searchStr: the search sequence to search for.
Return value
The function returns true if the character sequence contains a complete search sequence, ignoring the case considerations. Otherwise, it returns false.
Code
Example 1
- search string =
"AbC" - string =
"aBBbcCcAAaCcB"
Although "string" contains the same individual characters of "search string," it doesn’t contain the complete "search string" (ignoring the case considerations).
Hence, StringUtils.containsIgnoreCase would return false.
Example 2
- search string =
"aBC" - string =
"AbCdefghti"
Since "string" contains the complete "search string" (ignoring the case considerations), StringUtils.containsIgnoreCase would return true.
import org.apache.commons.lang3.StringUtils;public class Main{public static void main(String[] args){String characterSequence = "aBBbcCcAAaCcB";String searchString = "AbC";System.out.println(StringUtils.containsIgnoreCase(characterSequence, searchString));characterSequence = "AbC**defghti";searchString = "aBC";System.out.println(StringUtils.containsIgnoreCase(characterSequence, searchString));}}
Output
The output of the code will be as follows.
false
true