Java Reference
In-Depth Information
Display 3.4 Comparing Strings
1 public class StringComparisonDemo
2{
3
public static void main(String[] args)
4
{
5
String s1 = "Java isn't just for breakfast.";
6
String s2 = "JAVA isn't just for breakfast.";
7
if (s1.equals(s2))
8
System.out.println("The two lines are equal.");
9
else
10
System.out.println("The two lines are not equal.");
11
if (s2.equals(s1))
12
System.out.println("The two lines are equal.");
13
else
14
System.out.println("The two lines are not equal.");
15
if (s1.equalsIgnoreCase(s2))
16
System.out.println("But the lines are equal, ignoring case.");
17
else
18
System.out.println("Lines are not equal, even ignoring case.");
19
String s3 = "A cup of java is a joy forever.";
20
if (s3.compareToIgnoreCase(s1) < 0)
21
{
22
System.out.println("\"" + s3 + "\"");
23
System.out.println("precedes");
24
System.out.println("\"" + s1 + "\"");
25
System.out.println("in alphabetic ordering");
26
}
27
else
28
System.out.println("s3 does not precede s1.");
29
}
30
}
Sample Dialogue
The two lines are not equal.
The two lines are not equal.
But the lines are equal, ignoring case.
"A cup of java is a joy forever."
precedes
"Java isn't just for breakfast."
in alphabetic ordering
 
Search WWH ::




Custom Search