Search code examples
javastringsubstring

In Java, how do I check if a string contains a substring (ignoring case)?


I have two Strings, str1 and str2. How do I check if str2 is contained within str1, ignoring case?


Solution

  • str1.toUpperCase().contains(str2.toUpperCase())
    

    UPD:

    Original answer was using toLowerCase() method. But as some people correctly noticed, there are some exceptions in Unicode and it's better to use toUpperCase(). Because:

    There are languages knowing more than one lower case variant for one upper case variant.