| Return | Method | Summary |
|---|---|---|
| int | compareTo(String anotherString) | Compares two strings lexicographically. |
| int | compareToIgnoreCase(String str) | Compares two strings lexicographically, ignoring case differences. |
| boolean | equals(Object anObject) | Compares this string to the specified object. |
| boolean | equalsIgnoreCase(String anotherString) | Compares this String to another String, ignoring case considerations. |
The result of the compareTo(String anotherString) and compareToIgnoreCase(String str) is shown here:
| Value | Meaning |
|---|---|
| Less than zero | The invoking string is less than str. |
| Greater than zero | The invoking string is greater than str. |
| Zero | The two strings are equal. |
public class Main {
public static void main(String[] argv) {
String str = "Java2s.com";
String str2 = "java2s.com";
System.out.println(str.compareTo(str2));
System.out.println(str.compareToIgnoreCase(str2));
}
}
The output:
-32
0compareToIgnoreCase(String str) returns the same results as compareTo( ).
It only ignores the case differences.
public class Main {
public static void main(String args[]) {
System.out.println("A".compareToIgnoreCase("a"));
}
}
The output:
0
The following code checks the length of a string, then gets the fourth char out of the string object and compares these two string objects.
public class Main {
public static void main(String args[]) {
String strOb1 = "java2s.com";
String strOb2 = "java2s.com";
String strOb3 = strOb1;
System.out.println("Length of strOb1: " + strOb1.length());
System.out.println("Char at index 3 in strOb1: " + strOb1.charAt(3));
if (strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
System.out.println("strOb1 != strOb2");
if (strOb1.equals(strOb3))
System.out.println("strOb1 == strOb3");
else
System.out.println("strOb1 != strOb3");
}
}
This program generates the following output:
Length of strOb1: 10
Char at index 3 in strOb1: a
strOb1 == strOb2
strOb1 == strOb3The comparison is case-sensitive.
public class Main {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + s1.equalsIgnoreCase(s4));
}
}
The output:
Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true
To compare igoring case, call equalsIgnoreCase( ). It has this general form:
boolean equalsIgnoreCase(String str)
str is the String object being compared with the invoking String object.
Here is an example that demonstrates equals( ) and equalsIgnoreCase( ):
public class Main {
public static void main(String args[]) {
String s1 = "Hello";
String s2 = "Hello";
String s3 = "Good-bye";
String s4 = "HELLO";
System.out.println(s1 + " equals " + s2 + " -> " +
s1.equals(s2));
System.out.println(s1 + " equals " + s3 + " -> " + s1.equals(s3));
System.out.println(s1 + " equals " + s4 + " -> " + s1.equals(s4));
System.out.println(s1 + " equalsIgnoreCase " + s4 + " -> " + s1.equalsIgnoreCase(s4));
}
}
The output from the program is shown here:
Hello equals Hello -> true
Hello equals Good-bye -> false
Hello equals HELLO -> false
Hello equalsIgnoreCase HELLO -> true
java2s.com | | Contact Us | Privacy Policy |
| Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
| All other trademarks are property of their respective owners. |