esuslogo
 [To advertise Java(tm) Events here, contact joris@esus.com!]
banner

Java™
by example!






New @ Esus.com


  gb  In-house search engine for better results!

  gb  Get updates with the esus.com
newsletter!









  Home 
 Browse Categories 
 Ask a Java Question 
 Help 
  For Java Tips & Tricks, subscribe to the esus.com newsletter!
Search Java Q&A, Links, API's:   adv 

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:

This code sample is only viewable to esus.com members
Login or become a member!


outputs:

 
This code sample is only viewable to esus.com members
Login or become a member!


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!





Terms of Service | Privacy Policy | Contact

Copyright © 2000-2003 Esus.com - All Rights Reserved 
Java and all Java-based trademarks are trademarks of Sun Microsystems, Inc. Esus.com is independent of Sun Microsystems, Inc. All other trademarks are the sole property of their respective owners.