Lexicographical Comparison

Java-Lexicographical Comparison

Lexicographical Comparison

The equals( ) and equalsIgnoreCase( ) methods enable us to check whether two strings are identical or not. However, in some situations, we may wish to test whether one string is greater than, equal to or less than another string. The compareTo( ) method of the String class is useful in all such situations. The compareTo( ) method compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns ‘0’ exactly when the equals(Object) method would return true. For example:

Comparing Length

This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let ‘k’ be the smallest such index; then the string whose character at position ‘k’ has the smaller value, as determined by using the < operator, lexicographically precedes the other string. In this case, compareTo returns the difference of the two character values at position ‘k’ in the two string. For example:

Comparing Length

If there is no index position at which they differ, then the shorter string lexicographically precedes the  longer string. In this case, compareTo returns the difference of the lengths of the strings. For example:

Comparing Length

CompareToIgnoreCase

The compareToIgnoreCase( ) method compares two strings lexicographically, ignoring case differences. This method returns an integer whose sign is that of calling compareTo with normalized versions of the strings where case differences have been eliminated by converting all characters to lowercase. For example:

CompareToIgnoreCase

Program

Lexicographical Comparison


Program Source

public class Javaapp {

    public static void main(String[] args) {

        String str = new String("ABCDE");

        if (str.compareTo("ABCXY") == 0) {
            System.out.println("1 : Strings are equal");
        } else {
            System.out.println("1 : Strings are not equal");
        }
        if (str.compareTo("ABCDEFGH") == 0) {
            System.out.println("2 : Strings are equal");
        } else if (str.compareTo("ABCDEFGH") < 0) {
            System.out.println("2 : Lexicographically precedes the argument string.");
        } else {
            System.out.println("2 : Lexicographically follows the argument string.");
        }
        if (str.compareToIgnoreCase("abcDE") == 0) {
            System.out.println("3 : Strings are equal");
        } else {
            System.out.println("3 : Strings are not equal");
        }
    }
}

One thought on “Java-Lexicographical Comparison

Leave a Comment