|
Java™ by example!
|
|
|
What is a better way to compare Strings?
You could use the method compareTo which will return -1, 0 or 1 depending on whether the first String is (lexicographically) less, equal or greater than the second String. Main.java:
outputs:
Notice that compareTo is based on the Unicode value of each character in the Strings. There may be cases where you want to be less strict, for example if you want "cafe" and "café" to be compared as being equal. In that case, you need to use the locale sensitive and customizable Collator (and RuleBasedCollator) class that is part of the standard API. Check out this question/answer.
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|