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.
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.
StringUtils
You can import the StringUtils
class as follows.
import org.apache.commons.lang3.StringUtils;
public static boolean containsIgnoreCase(final CharSequence str, final CharSequence searchStr)
final CharSequence str
: the character sequence to search in.
final CharSequence searchStr
: the search sequence to search for.
The function returns true
if the character sequence contains a complete search sequence, ignoring the case considerations. Otherwise, it returns false
.
"AbC"
"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
.
"aBC"
"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));}}
The output of the code will be as follows.
false
true