Alex Rivera | Logout

Java: String: equalsIgnoreCase vs switching everything to Upper/Lower Case

Asked 2010-12-15T04:17:53.310
27

It came to my attention that there a several ways to compare strings in Java.

I just got in the habit ages ago to use equalsIgnoreCase to avoid having problems with case sensitive strings.

Others on the other hand prefer passing everything in upper or lower case.

From where I stand (even if technically I'm sitting), I don't see a real difference.

Does anybody know if one practice is better than the other? And if so why?

Edit
Report

1 Answer

2

Neither is better, they both have their uses in different scenarios.

Many times when you have to do string comparisons there is the opportunity to massage at least one of the strings to make it easier to compare, and in these cases you will see strings converted to a particular case, trimmed, etc before being compared.

If, on the other hand, you just want to do an on-the-fly case-insensitive comparison of two strings then feel free to use equalsIgnoreCase, that's what its there for after all. I would caution, however, that if you're seeing a lot of equalsIgnoreCase it could be a code smell.

answered 2010-12-15T10:58:44.190

Your Answer