In a java app, assuming I have option of choosing the following comparison methods
equalsIgnoreCase(String anotherString)
compareToIgnoreCase(String str)
Which one is faster?
equalsIgnoreCase
can be a lot faster. For example, consider two strings which start with the same 10,000 characters - but one of them has an extra character at the end. equalsIgnoreCase
can return immediately; compareToIgnoreCase
has to iterate to the end of the string to see the difference.
But generally I'd go with whichever expresses your intention better. This works well for performance too: assuming I'm right in saying that equalsIgnoreCase
is at least as fast as compareToIgnoreCase
, it means you should use that where you can - if you need an actual ordering, you've got to use compareToIgnoreCase
anyway.
if you worry about performances... measure it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With