Case insensitive string comparisons should be made without intermediate upper or lower casing
Using toLowerCase()
or toUpperCase()
to make case insensitive comparisons is inefficient because it requires the creation of temporary, intermediate String
objects.
The following code:
boolean result1 = foo.toUpperCase().equals(bar); // Non-Compliant boolean result2 = foo.equals(bar.toUpperCase()); // Non-Compliant boolean result3 = foo.toLowerCase().equals(bar.LowerCase()); // Non-Compliant
should be refactored into:
boolean result = foo.equalsIgnoreCase(bar); // Compliant