Comparing Strings


boolean result = str1.equals(str2); boolean result2 = str1.equalsIgnoreCase(str2);



The value of result and result2 will be true if the strings contain the same content. If the strings contain different content, the value of result and result2 will be false. The first method, equals(), is case sensitive. The second method, equalsIgnoreCase(), will ignore the case of the strings and return true if the content is the same regardless of case.

String comparison is a common source of bugs for novice Java programmers. A novice programmer will often attempt to compare strings using the comparison operator ==. When used with Strings, the comparison operator == compares object references, not the contents of the object. Because of this, two string objects that contain the same string data, but are physically distinct string object instances, will not compare as equal when using the comparison operator.

The equals() method on the String class compares a string's contents, rather than its object reference. This is the preferred string comparison behavior in most string comparison cases. See the following example:

String name1 = new String("Timmy"); String name2 = new String("Timmy"); if (name1 == name2) {      System.out.println("The strings are equal."); } else {      System.out.println("The strings are not equal."); }


The output from executing these statements will be

The strings are not equal.


Now use the equals() method and see the results:

String name1 = new String("Timmy"); String name2 = new String("Timmy"); if (name1.equals(name2)) {      System.out.println("The strings are equal."); } else {      System.out.println("The strings are not equal."); }


The output from executing these statements will be

The strings are equal.


Another related method on the String class is the compareTo() method. The compareTo() method compares two strings lexographically, returning an integer valueeither positive, negative, or 0. The value 0 is returned only if the equals() method would evaluate to true for the two strings. A negative value is returned if the string on which the method is called alphabetically precedes the string that is passed as a parameter to the method. A positive value is returned if the string on which the method is called alphabetically comes after the string that is passed as a parameter. To be precise, the comparison is based on the Unicode value of each character in the strings being compared. The compareTo() method also has a corresponding compareToIgnoreCase() method that performs functionally the same with the exception that the characters' case is ignored. See the following example:

String name1="Camden"; String name2="Kerry"; int result = name1.compareTo(name2); if (result == 0) {      System.out.println("The names are equal."); } else if (result > 0) {      System.out.println(          "name2 comes before name1 alphabetically."); } else if (result < 0) {      System.out.println(         "name1 comes before name2 alphabetically."); }


The output of this code will be

name1 comes before name2 alphabetically.





JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net