Using toLowerCase()
or toUpperCase()
to make case insensitive comparisons is inefficient because it requires the creation
of temporary, intermediate String
objects.
boolean result1 = foo.toUpperCase().equals(bar); // Noncompliant boolean result2 = foo.equals(bar.toUpperCase()); // Noncompliant boolean result3 = foo.toLowerCase().equals(bar.LowerCase()); // Noncompliant
boolean result = foo.equalsIgnoreCase(bar); // Compliant
No issue will be raised when a locale is specified because the result could be different from "equalsIgnoreCase". (e.g.: using the Turkish locale)
boolean result1 = foo.toUpperCase(locale).equals(bar); // Compliant