String Comparison
Java String class provides methods for comparing two strings such as equals(), equalsIgnoreCase(), compareTo().
| Method | Purpose |
|---|---|
| compareTo(String) | Compares two strings. Returns 0 if they are equal, a negative value if the specified string is greater than the string or a positive value otherwise. |
| compareToIgnoreCase(String) | Compares two strings, ignoring case differences |
| equals(Object) | Returns true if the string matches the object. |
| equalsIgnoreCase(String) | returns true if the argument is not null and it represents an equivalent String ignoring case |
| endsWith(String) | Returns true if the string ends with the specified string. |
| startsWith(String) | Returns true if the string starts with the specified string. |
| startsWith(String, int offset) | Returns true if the string starts with the specified string at the specified offset. |
| regionMatches(int toffset,String other, int ooffset, int len) | Returns true if the specified region of the string matches the specified region of a different string. where, toffset – the starting offset of the subregion in this string. other – the string argument. ooffset – the starting offset of the subregion in the string argument. len – the number of characters to compare. |
| regionMatches(boolean ignoreCase, int toffset, String other, int ooffset, int len) | Returns true if the specified region of the string matches the specified region of a different string. where, ignoreCase – if true, ignore case when comparing characters. toffset – the starting offset of the subregion in this string. other – the string argument. ooffset – the starting offset of the subregion in the string argument. len – the number of characters to compare. |
Java String comparison with compareTo()
int compareTo(String anotherString)
int compareToIgnoreCase(String anotherString)
These two methods return,
0, if the argument string is equal to this string;
negative integer, if this string is less than the string argument;
positive integer, if this string is greater than the string argument.
package com.ibytecode.strings.methods;
public class ComparingStrings {
public static void main(String[] args) {
String ONE = "ONE";
String One = "One";
// compareTo(String)
System.out.println(ONE.compareTo(One));
System.out.println(ONE.compareTo(ONE));
System.out.println(One.compareTo(ONE));
System.out.println(One.compareToIgnoreCase(ONE));
}
}
-32
0
32
0
Java String comparison with equals()
public boolean equals(Object anObject)
public boolean equalsIgnoreCase(String anotherString)
package com.ibytecode.strings.methods;
public class ComparingStrings {
public static void main(String[] args) {
String ONE = "ONE";
String One = "One";
System.out.println("ONE.equals(One): " + ONE.equals(One));
System.out.println("ONE.equalsIgnoreCase(One): "
+ ONE.equalsIgnoreCase(One));
}
}
ONE.equals(One): false
ONE.equalsIgnoreCase(One): true
String ends with specified string
package com.ibytecode.strings.methods;
public class ComparingStrings {
public static void main(String[] args) {
String str = "My favorite language is Java";
boolean result1 = str.endsWith("Java");
System.out.println(result1);
}
}
true
String starts with specified string
package com.ibytecode.strings.methods;
public class ComparingStrings {
public static void main(String[] args) {
String str = "My favorite language is Java";
boolean result2 = str.startsWith("My");
// see if starting in offset 24 str starts with "Java"
boolean result3 = str.startsWith("Java", 24);
System.out.println(result2);
System.out.println(result3);
}
}
true
true
String region matches
package com.ibytecode.strings.methods;
public class ComparingStrings {
public static void main(String[] args) {
String str = "My favorite language is Java";
String str2 = new String("I like the Java language");
String str3 = new String("I LIKE THE JAVA LANGUAGE");
/* compare regions. Start at offset 24 in str and
compare against offset 11 in str2 for 4 characters */
boolean regionMatch1 = str.regionMatches(24, str2, 11, 4);
boolean regionMatch2 = str3.regionMatches(true, 11, str2, 11, 4);
boolean regionMatch3 = str3.regionMatches(false, 11, str2, 11, 4);
System.out.println(regionMatch1);
System.out.println(regionMatch2);
System.out.println(regionMatch3);
}
}
true
true
false
