Explain Codes LogoExplain Codes Logo

How to check if a String contains another String in a case insensitive manner in Java?

java
string-comparison
performance-optimization
java-8
Alex KataevbyAlex Kataev·Dec 29, 2024
TLDR

Here is a fast, simple solution using equalsIgnoreCase() along with contains() for a case-insensitive substring search:

String fullStr = "Case Insensitive Check"; String subStr = "case"; boolean result = fullStr.toUpperCase().contains(subStr.toUpperCase());

In this code, we convert both fullStr and subStr to uppercase, making the search case-insensitive.

A helping hand from regionMatches

The regionMatches() method can be a performance-friendly alternative. This method avoids generating extra objects and can improve speed.

boolean result = fullStr.regionMatches(true, fullStr.indexOf(subStr), subStr, 0, subStr.length());

Now your code is as fast as Usain Bolt... well, almost.

Pattern and Matcher dance

Java's Pattern.compile allows for case-insensitive checks.

Pattern pattern = Pattern.compile(Pattern.quote(subStr), Pattern.CASE_INSENSITIVE); boolean result = pattern.matcher(fullStr).find();

This code compiles a pattern with CASE_INSENSITIVE, ensuring that "Java" and "java" are treated the same. We also use Pattern.quote, acting like Harry Potter's invisibility cloak for any regex special characters within subStr.

Calling Apache for backup

The StringUtils.containsIgnoreCase method from Apache Commons Lang provides a compact and efficient check:

boolean result = StringUtils.containsIgnoreCase(fullStr, subStr);

It's like having sumo wrestlers do your heavy lifting: efficient and saves your energy.

To toLowerCase() or not to toLowerCase()

Avoid using toLowerCase() or toUpperCase() for string comparison. Overuse may result in a performance hit.

Cache in on Patterns

For repeated checks, cache your Pattern objects to save recompilation time:

Pattern pattern = Pattern.compile(Pattern.quote(subStr), Pattern.CASE_INSENSITIVE); // Now this pattern is as re-usable as your favorite shopping bag.

Note that String.regionMatches can still outperform this in some scenarios.

Don't get tangled in Regex

Regex carries significant overhead due to its complexity. Consider methods like regionMatches before reaching for regex to ensure faster execution.

Important aspects to consider

  • Memory usage vs Performance: Consider these attributes when choosing your string comparison method.
  • Pattern Reuse: If using regex, precompile and reuse Pattern objects, helping your code run smoother than a lubed slip-and-slide.
  • Libraries are your friend: Using trusted libraries like Apache Commons cuts down on homemade code.
  • Choose the right tool: RegEx is powerful, but can be misunderstood. Ensure you understand regex before opting for it.

Go fast with regionMatches

Remember, regionMatches could be your speedy solution, especially in performance-critical applications. It's the caffeine shot your code needs.

Careful with those special characters

When using regex, ensure proper escaping of special regex characters using Pattern.quote. It acts like a safety net for your acrobatic regex stunts.