Java String compareTo method with examples

Signature: public int compareTo(String anotherString)
Sample usage :strone.compareTo(strtwo) ,“thisString”.compareTo(“anotherString”)

This method compares two Strings lexicographically based on the Unicode value of each character in each of the Strings and returns a negative integer if this String e.g strone  ,“thisString” above lexicographically precedes the argument String e.g strtwo , “anotherString” above, a positive integer if the argument string e.g strtwo, “anotherString”  lexicographically precedes this string e.g strone, “thisString”. and zero(0) if both Strings are lexicographically equal or the String equals(public boolean equals(Object anotherObject)) returns true as this implies the strings are equal in sequence and character case(comparesTo is case sensitive).

How String compareTo method works/how lexicographic ordering works

Comparison works character by character at each index in both strings at the same time.If both characters at this position are equal,comparison advances to the next index.If the characters are different(completely different characters or same character with different cases e.g ‘A’ and ‘a’), two scenarios arise.One,if the decimal value of the character at this index of this string ,e.g strone above is less than the decimal value of the character at this index of the argument string e.g strtwo above,then this string precedes the argument string and a negative integer ,the difference of the decimal values is returned.Two,if the decimal value of the character at this index in this string is greater than the decimal value of the character at this index in the argument string,then a positive integer is returned.

If both strings do not differ at all (comparison up to the last characters in the last index),then a 0 is returned.

If both strings are of different lengths and this is the shorter string and  is a substring of the longer string(the argument string),then at no position do they differ and the difference in length of this string and the argument string is returned i.e .a negative result will be returned implying this string precedes the argument string.The inverse is true whereby this has a greater length than the argument string with a positive int being returned.

 

*Decimal value of a character-chars can be treated as ints. They can be added or subtracted and the result is always an int.A character’s value can be converted to an int by type casting for example System.out.println((int)'k') outputs 107.Thefefore 107 is the decimal value/equivalent of char ‘k’.

A table of characters ,their Unicode and Decimal values is available at en.wikipedia.org/wiki/List_of_Unicode_characters.

Java String compareTo() examples

Example 1

String strone=“java”;

String strtwo=“java”;

The invocation strone.compareTo(strtwo) returns 0 .

The two strings are equal in length and character composition from first to last index.They are the same so we return 0.

Example 2

String strone=“onetime”;

String strtwo=“onceuponatime”;

 

The invocation strone.compareTo(strtwo) returns 2

Explanation:

The first 2 indices contain same charactes.At index 2(3rd position) we have ‘e’ (unicode value \u0065 decimal value 101) in strone and ‘c’ (unicode value \u0063 decimal value 99) ,the difference 101-99=2.This is same as strone.charAt(2)-strtwo.charAt(2).

Example 3

String strone=“onetime”;

String strtwo=“oneTime”;

 

The invocation strone.compareTo(strtwo) returns 32

Explanation:

The first 3 indices contain same charactes.At index 3(4th position) we have ‘t’ (unicode value \u0074,decimal value 116) in strone and ‘T’ (unicode value \u0054,decimal value 84) ,the difference 116-84=32.

 

Example 4

String strone=“green”;

String strtwo=“blue”;

 

The invocation strone.compareTo(strtwo) returns 5.

Explanation:

At index 0(first position),we have two characters ‘g’(unicode value \u0067,Decimal value 103) and ‘b’(unicode value \u0062 ,Decimal value 98).The difference 103-98 =5 is returned.

 

Example 5

String strone=“green”;

String strtwo=“gReeN”;

 

The invocation strone.compareTo(strtwo) returns 32

Explanation:

At index 1(second position),we have two characters ‘r’(unicode value \u0072,Decimal value 114) and ‘R’(unicode value \u0052 ,Decimal value 82).The difference 114-82=32 is returned.

 

 

 

Example 6

String strone=“dream”;

String strtwo=“dreamteam”;

 

The invocation strone.compareTo(strtwo) returns -4

Explanation:

We have same characters at all indices starting from 0-5 i.e characters d to m.Since the two strings are the same upto to end of the shorter string,strone,we return the difference in length of the two strings i.e strone has length 5 and strtwo has length 9 therefore 5-9 = -4 is returned.

 

 

 

Example 7

String strone=“cheerleader”;

String strtwo=“cheer”;

 

The invocation strone.compareTo(strtwo) returns 6

Explanation:

The two strings are similar in character composition upto index 4.Therefore in such a scenario,we return the difference in length of strone.length()-strtwo.length().String strone has length 11 and strtwo has length 5 so we return 11-5=6.

 

Example 8

String strone=“Cheerleader”;

String strtwo=“cheer”;

 

The invocation strone.compareTo(strtwo) returns -32

Explanation:

At index 0 we have ‘C’(unicode value \u0043,Decimal value 67) and ‘c’(Unicode value \u0063 ,Decimal value 99).We therefore return 67-99 =-32.

 

Example 9

String strone=“cheer”;

String strtwo=“Cheerleader”;

The invocation strone.compareTo(strtwo) returns 32

At index 0 we have  ‘c’(Unicode value \u0063 ,Decimal value 99)  and ‘C’(unicode value \u0043,Decimal value 67).We therefore return 99-67=32.

About the Author - John Kyalo Mbindyo(Bsc Computer Science) is a Senior Application Developer currently working at NCBA Bank Group,Nairobi- Kenya.He is passionate about making programming tutorials and sharing his knowledge with other software engineers across the globe. You can learn more about him and follow him on  Github.