+1 vote
in JAVA by
How to compare two Strings in java program?

1 Answer

0 votes
by

Java String implements Comparable interface and it has two variants of compareTo() methods.

compareTo(String anotherString) method compares the String object with the String argument passed lexicographically. If String object precedes the argument passed, it returns negative integer and if String object follows the argument String passed, it returns a positive integer. It returns zero when both the String have the same value, in this case equals(String str) method will also return true.

compareToIgnoreCase(String str): This method is similar to the first one, except that it ignores the case. It uses String CASE_INSENSITIVE_ORDER Comparator for case insensitive comparison. If the value is zero then equalsIgnoreCase(String str) will also return true.

Check this post for String compareTo example.

Related questions

0 votes
asked Mar 19, 2020 in PHP by DavidAnderson
0 votes
asked Nov 25, 2020 in JAVA by SakshiSharma
...