Java String CompareTo Sample Code

This tutorial will explore two methods of the Java String class called compareTo() and compareToIgnoreCase(). Both methods compare two strings lexicographically.

So what is lexicographical? Good question. Basically in this environment it would be like comparing the order of the words in a dictionary. The comparison is based on the Unicode value of each character in both strings.

The compareTo() method takes a string to compare as the only argument. This string method will return an integer value.

The compareToIgnorCase() method behaves the same way as compareTo(), except that this method will ignore case. Case is referring to upper or lower case characters in the string.

Method return values:

  • = 0 (if the string is equal to the string argument)
  • < 0 (if this string is less than the string argument)
  • > 0 (if this string is greater than the string argument)


String compareTo

In this sample code we are using compareTo() method to compare myStringA to myStringB, myStringC, and myStringD. The first comparison returns a -1 because “XYZ” comes before “XZZ”. The second comparison returns a 1 because “XYZ” comes after “XYY”. The final comparison returns a 0 because the strings are equal.

package com.jcd.java.tutorials;

public class JavaStringCompareTo 
{

	public static void main(String[] args) 
	{
		
		String myStringA = "XYZ";
		String myStringB = "XZZ";
		String myStringC = "XYY";
		String myStringD = "XYZ";

		
		System.out.println("Compare myStringA To myStringB: "+myStringA.compareTo(myStringB));
		System.out.println("Compare myStringA To myStringC: "+myStringA.compareTo(myStringC));
		System.out.println("Compare myStringA To myStringD: "+myStringA.compareTo(myStringD));

	}

}
Compare myStringA To myStringB: -1
Compare myStringA To myStringC: 1
Compare myStringA To myStringD: 0


String compareToIgnoreCase

In this sample code that utilizes compareToIgnoreCase() method, we compare two strings like in the previous sample, but in this instance the case of each string is ignored. As you see below, myStringA and myStringB will return 0 because they are the same string minus the case.

ackage com.jcd.java.tutorials;

public class JavaStringCompareTo 
{

	public static void main(String[] args) 
	{
			
		String myStringA = "XyZ";
		String myStringB = "xzZ";
		String myStringC = "Xyy";
		String myStringD = "xYz";

		
		System.out.println("Compare myStringA To myStringB Ignore Case: "+myStringA.compareToIgnoreCase(myStringB));
		System.out.println("Compare myStringA To myStringC Ignore Case: "+myStringA.compareToIgnoreCase(myStringC));
		System.out.println("Compare myStringA To myStringD Ignore Case: "+myStringA.compareToIgnoreCase(myStringD));

	}

}
Compare myStringA To myStringB Ignore Case: -1
Compare myStringA To myStringC Ignore Case: 1
Compare myStringA To myStringD Ignore Case: 0


Concluding Comments

If you need to compare two strings in Java and case is important, the compareTo() method is the way to go. On the other hand, if you don’t care about case, using the compareToIgnoreCase() method is a good choice.



Java References:

Add a Comment

Your email address will not be published. Required fields are marked *