Skip to main content

Compare two strings in Java lexicographically

Compare two strings lexicographically
The compareTo(T ob) method in the Comparable class is implemented in the String class and this implemented method takes java.lang.String as parameter. This method compares two strings lexicographically. The term lexicographically might seem new and weird. Whatever, don't get worried. I'll explain this method in a very easy way.

The compareTo method in the String class compares two strings. Here two sample cases,

1. When both of the strings differ in characters at a particular position then characters of both the strings are checked in order whether they match are not i.e. the 'i'th character of one string is checked with the 'i'th character of another string for equality (whether they are equal or not) starting from i=0 till the end of the smallest string (provided in both the strings there is 'i'th character

st.charAt(i)-st1.charAt(i); 
The above statement here is returned where st is the object in which compareTo() is called and st1 is the parameter for compareTo(). You'll have to note that though the Strings differ in length, the length property is not given the first preference i.e. the difference in the ascii values of the characters is returned but not the difference in lengths.

2. When both of the strings does not differ in characters at same positions, the difference of their lengths is returned. The length of the String object which is passed to the compareTo() method is subtracted from the length of the String object in which this method is called. Here, right from the starting of the string every character of the string is checked whether it matches or not. In this case, every character is matched and only the lengths differ. Consider an example of two such strings for a better understand.


abcd
abcde

Here the first string is a part of the second string and the second string contains the character e in addition.

When we consider the string is a set of characters in order, then first string is a subset of the second string. So for the compareTo(String) method to return difference in lengths, one string should be subset of the other.

Here when the compareTo method is called from the first string passing the second string as parameter, then -1 is returned i.e. length of first string - length of second string.


class CompareToExample
{
    public static void main(String args[])
    {
    String st="abcde";
    String st1="abcdf";
   
    System.out.println(st.compareTo(st1));
    }
}

Output


-1

Explanation

Here compareTo() method is called from the st and st1 is sent as parameter.
The first 4 characters of both the strings are identical but the last character differ as a result, ascii of value of e - ascii value of f is returned. As e and f have consecutive ascii values and e being smaller than f in terms of ascii value, e-f gives rise to -1. If reverse is the case, however 1 is returned (for example).

Simple. Isn't it. If you still have a doubt on the compareTo(String) method feel free to drop a comment. I'll respond as soon as possible.

Comments

Popular posts from this blog

Setting Background Image in JFrame - Swing

Here is sample tutorial, a simple trick that enables you to set background image for JFrame.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class BackgroundImageJFrame extends JFrame
{
JButton b1;
JLabel l1;
public BackgroundImageJFrame()
{
setTitle("Background Color for JFrame");
setSize(400,400);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
/*
One way
-----------------
setLayout(new BorderLayout());
JLabel background=new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png"));
add(background);
background.setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am a button");
background.add(l1);
background.add(b1);
*/
// Another way
setLayout(new BorderLayout());
setContentPane(new JLabel(new ImageIcon("C:\\Users\\Computer\\Downloads\\colorful design.png")));
setLayout(new FlowLayout());
l1=new JLabel("Here is a button");
b1=new JButton("I am …

Add two numbers in JSP easily

Here is an example on adding up two numbers in JSP. This is very simple as you see below. This requires very small piece of code. All i have done here is created a HTML file containing the input fields which contain the numbers to add up.Now the values in the input field are of type String, so i have done type casting and converted them into int using Integer.parseInt(). See how easy it looks.



Catch The Rat: Simple Game in Java

A simple, shortest and easiest game that could be written in Java using Swing components and event handling.