public class StringCompare
1: // Fig. 29.3: StringCompare.java
2: // String methods equals, equalsIgnoreCase, compareTo and regionMatches.
3:
4: public class StringCompare
5: {
6: public static void main( String args[] )
7: {
8: String s1 = new String( "hello" ); // s1 is a copy of "hello"
9: String s2 = "goodbye";
10: String s3 = "Happy Birthday";
11: String s4 = "happy birthday";
12:
13: System.out.printf(
14: "s1 = %s\ns2 = %s\ns3 = %s\ns4 = %s\n\n", s1, s2, s3, s4 );
15:
16: // test for equality
17: if ( s1.equals( "hello" ) ) // true
18: System.out.println( "s1 equals \"hello\"" );
19: else
20: System.out.println( "s1 does not equal \"hello\"" );
21:
22: // test for equality with ==
23: if ( s1 == "hello" ) // false; they are not the same object
24: System.out.println( "s1 is the same object as \"hello\"" );
25: else
26: System.out.println( "s1 is not the same object as \"hello\"" );
27:
28: // test for equality (ignore case)
29: if ( s3.equalsIgnoreCase( s4 ) ) // true
30: System.out.printf( "%s equals %s with case ignored\n", s3, s4 );
31: else
32: System.out.println( "s3 does not equal s4" );
33:
34: // test compareTo
35: System.out.printf(
36: "\ns1.compareTo( s2 ) is %d", s1.compareTo( s2 ) );
37: System.out.printf(
38: "\ns2.compareTo( s1 ) is %d", s2.compareTo( s1 ) );
39: System.out.printf(
40: "\ns1.compareTo( s1 ) is %d", s1.compareTo( s1 ) );
41: System.out.printf(
42: "\ns3.compareTo( s4 ) is %d", s3.compareTo( s4 ) );
43: System.out.printf(
44: "\ns4.compareTo( s3 ) is %d\n\n", s4.compareTo( s3 ) );
45:
46: // test regionMatches (case sensitive)
47: if ( s3.regionMatches( 0, s4, 0, 5 ) )
48: System.out.println( "First 5 characters of s3 and s4 match" );
49: else
50: System.out.println(
51: "First 5 characters of s3 and s4 do not match" );
52:
53: // test regionMatches (ignore case)
54: if ( s3.regionMatches( true, 0, s4, 0, 5 ) )
55: System.out.println( "First 5 characters of s3 and s4 match" );
56: else
57: System.out.println(
58: "First 5 characters of s3 and s4 do not match" );
59: } // end main
60: } // end class StringCompare
61:
62: /**************************************************************************
63: * (C) Copyright 1992-2005 by Deitel & Associates, Inc. and *
64: * Pearson Education, Inc. All Rights Reserved. *
65: * *
66: * DISCLAIMER: The authors and publisher of this book have used their *
67: * best efforts in preparing the book. These efforts include the *
68: * development, research, and testing of the theories and programs *
69: * to determine their effectiveness. The authors and publisher make *
70: * no warranty of any kind, expressed or implied, with regard to these *
71: * programs or to the documentation contained in these books. The authors *
72: * and publisher shall not be liable in any event for incidental or *
73: * consequential damages in connection with, or arising out of, the *
74: * furnishing, performance, or use of these programs. *
75: *************************************************************************/