In Java, how do I check if a string contains a substring (ignoring case)?
To check a string for a substring without considering case, use the toLowerCase()
method on both strings in combination with contains()
:
Why uppercase can be a safer bet
However, one should favor toUpperCase()
over toLowerCase()
, as in some languages the translation from uppercase to lowercase has multiple variants for the same letter:
Regular Expression for more flexibility
If there's a need for more adaptability, Regular expressions allow us to find substrings case-insensitively flexibly:
Be smart with Apache Commons Lang
Apache Commons Lang, an open-source utility library, offers a set of useful string operations including the method StringUtils.containsIgnoreCase()
:
Customize comparison logic for the win
For cases when you don't want to depend on external libraries, you could implement a custom method with careful handling for null values:
Performance does matter
When dealing with huge Strings, performance could be a concern. Be mindful about using toLowerCase()
or toUpperCase()
with contains()
and indexOf()
while performing case-insensitive check.
Edge cases are often ignored
Avoid errors due to unexpected circumstances like a shorter substring length than the string or null
values when matching strings character-by-character.
Reusability for future flexibility
Encapsulate frequent use cases in reusable methods, promotes future code flexibility and maintenance.
Respecting null values enhances robustness
A well-behaved method takes care of null
inputs to prevent any uncalled for NullPointerExceptions
:
Going literal with regex for efficiency
When working with regex, go for Pattern.CASE_INSENSITIVE + Pattern.LITERAL
for efficient matching:
Right tool for the right job
Sometimes, a contains()
might seem like an easy bet, but a specific case might demand the use of startsWith()
or endsWith()
for precise execution:
Was this article helpful?