public class StringComparisons
2: import java.util.Scanner;
4: /**
5: * A program that compares String values.
6: *
7: * @author Mark Young (A00000000)
8: */
9: public class StringComparisons {
11: public static void main(String[] args) {
12: // create variables
13: Scanner kbd = new Scanner(System.in);
14: String s1, s2, s3, s4, s5, s6, s7, s8, s9;
16: // initialize them
17: s1 = "First String";
18: s2 = "Second String";
19: s3 = "first string";
20: s4 = "FirstString";
21: s5 = "First String ";
23: // get input and pause
24: System.out.print("Enter the String \"First String\" "
25: + "(without the quotes): ");
26: s6 = kbd.nextLine();
27: System.out.print("Press enter...");
28: kbd.nextLine();
29: System.out.println();
31: // report whether Strings are ==
32: System.out.println("\"" + s1 + "\" == \"" + s2 + "\" is "
33: + (s1 == s2));
34: System.out.println("\"" + s1 + "\" == \"" + s3 + "\" is "
35: + (s1 == s3));
36: System.out.println("\"" + s1 + "\" == \"" + s4 + "\" is "
37: + (s1 == s4));
38: System.out.println("\"" + s1 + "\" == \"" + s5 + "\" is "
39: + (s1 == s5));
40: System.out.println("\"" + s1 + "\" == \"" + s6 + "\" is "
41: + (s1 == s6));
42: System.out.print("Press enter...");
43: kbd.nextLine();
44: System.out.println();
46: // report whether Strings are equals
47: System.out.println("\"" + s1 + "\".equals(\"" + s2 + "\") is "
48: + (s1.equals(s2)));
49: System.out.println("\"" + s1 + "\".equals(\"" + s3 + "\") is "
50: + (s1.equals(s3)));
51: System.out.println("\"" + s1 + "\".equals(\"" + s4 + "\") is "
52: + (s1.equals(s4)));
53: System.out.println("\"" + s1 + "\".equals(\"" + s5 + "\") is "
54: + (s1.equals(s5)));
55: System.out.println("\"" + s1 + "\".equals(\"" + s6 + "\") is "
56: + (s1.equals(s6)));
57: System.out.print("Press enter...");
58: kbd.nextLine();
59: System.out.println();
61: // report whether Strings are equal ignoring case
62: System.out.println("\"" + s1 + "\".equalsIgnoreCase(\"" + s2 + "\") is "
63: + (s1.equalsIgnoreCase(s2)));
64: System.out.println("\"" + s1 + "\".equalsIgnoreCase(\"" + s3 + "\") is "
65: + (s1.equalsIgnoreCase(s3)));
66: System.out.println("\"" + s1 + "\".equalsIgnoreCase(\"" + s4 + "\") is "
67: + (s1.equalsIgnoreCase(s4)));
68: System.out.println("\"" + s1 + "\".equalsIgnoreCase(\"" + s5 + "\") is "
69: + (s1.equalsIgnoreCase(s5)));
70: System.out.println("\"" + s1 + "\".equalsIgnoreCase(\"" + s6 + "\") is "
71: + (s1.equalsIgnoreCase(s6)));
72: System.out.println();
74: // report which strings start with "First"
75: s7 = "First";
76: System.out.println("\"" + s1 + "\".startsWith(\"" + s7 + "\") is "
77: + (s1.startsWith(s7)));
78: System.out.println("\"" + s2 + "\".startsWith(\"" + s7 + "\") is "
79: + (s2.startsWith(s7)));
80: System.out.println("\"" + s3 + "\".startsWith(\"" + s7 + "\") is "
81: + (s3.startsWith(s7)));
82: System.out.println("\"" + s4 + "\".startsWith(\"" + s7 + "\") is "
83: + (s4.startsWith(s7)));
84: System.out.println("\"" + s5 + "\".startsWith(\"" + s7 + "\") is "
85: + (s5.startsWith(s7)));
86: System.out.println("\"" + s6 + "\".startsWith(\"" + s7 + "\") is "
87: + (s6.startsWith(s7)));
88: System.out.print("Press enter...");
89: kbd.nextLine();
90: System.out.println();
92: // report which strings end with "String"
93: s8 = "String";
94: System.out.println("\"" + s1 + "\".endsWith(\"" + s8 + "\") is "
95: + (s1.endsWith(s8)));
96: System.out.println("\"" + s2 + "\".endsWith(\"" + s8 + "\") is "
97: + (s2.endsWith(s8)));
98: System.out.println("\"" + s3 + "\".endsWith(\"" + s8 + "\") is "
99: + (s3.endsWith(s8)));
100: System.out.println("\"" + s4 + "\".endsWith(\"" + s8 + "\") is "
101: + (s4.endsWith(s8)));
102: System.out.println("\"" + s5 + "\".endsWith(\"" + s8 + "\") is "
103: + (s5.endsWith(s8)));
104: System.out.println("\"" + s6 + "\".endsWith(\"" + s8 + "\") is "
105: + (s6.endsWith(s8)));
106: System.out.print("Press enter...");
107: kbd.nextLine();
108: System.out.println();
110: // show proper use of toUpperCase and toLowerCase
111: System.out.println("\"" + s1 + "\".toUpperCase() is \""
112: + s1.toUpperCase() + "\"");
113: System.out.println("\"" + s1 + "\" is still \"" + s1 + "\"");
114: System.out.println("\"" + s1 + "\".toLowerCase() is \""
115: + s1.toLowerCase() + "\"");
116: System.out.println("\"" + s1 + "\" is still \"" + s1 + "\"");
117: System.out.print("Press enter...");
118: kbd.nextLine();
119: System.out.println();
121: // Show how to do startsWith ignoring case
122: s9 = s7.toUpperCase();
123: System.out.println("\"" + s1 + "\".toUpperCase().startsWith(\""
124: + s9 + "\") is " + (s1.toUpperCase().startsWith(s9)));
125: System.out.println("\"" + s2 + "\".toUpperCase().startsWith(\""
126: + s9 + "\") is " + (s2.toUpperCase().startsWith(s9)));
127: System.out.println("\"" + s3 + "\".toUpperCase().startsWith(\""
128: + s9 + "\") is " + (s3.toUpperCase().startsWith(s9)));
129: System.out.println("\"" + s4 + "\".toUpperCase().startsWith(\""
130: + s9 + "\") is " + (s4.toUpperCase().startsWith(s9)));
131: System.out.println("\"" + s5 + "\".toUpperCase().startsWith(\""
132: + s9 + "\") is " + (s5.toUpperCase().startsWith(s9)));
133: System.out.println("\"" + s6 + "\".toUpperCase().startsWith(\""
134: + s9 + "\") is " + (s6.toUpperCase().startsWith(s9)));
135: System.out.print("Press enter...");
136: kbd.nextLine();
137: System.out.println();
138: }
140: }