Compare Strings Alphabetically Java

Compare Strings Alphabetically Java | Here, in this blog, we will discuss how to compare two strings alphabetically in Java. We can use the compareTo() method available in the Java String class to do this.

To understand more see the below examples:
1) string 1= “java”
string2= “java”
Output:- Both strings are equal
2)string1= “Java”
string2 = “java”
Output:- Both strings are not equal

Compare Strings Alphabetically Java Using compareTo()

The method details for the compareTo() method are as follows:-
public String compareTo(String str)
Here, str-: the string needs to be compared.
The compareTo() method returns following values:-

  • 0 if both strings are equal
  • <0 if this string is lexicographically less than the string argument.
  • >0 if this string is lexicographically greater than the string argument.

In this code, we demonstrate the use of the compareTo() method. Observe the output comparing strings Java alphabetically. The compareTo() code is case sensitive that is it considers the small letters and capital letters as different. Let us see it through an example.

Java Compare Strings Alphabetically using compareTo()

public class Main {

   public static void main(String args[]) {
      String str1 = "java";
      String str2 = "Java";
      compareStrings(str1, str2);

      String str3 = "programming";
      String str4 = "Programming";
      compareStrings(str3, str4);

      String str5 = "language";
      String str6 = "Language";
      compareStrings(str5, str6);

      String str7 = "Know";
      String str8 = "Program";
      compareStrings(str7, str8);

      String str9 = "Coding";
      String str10 = "program";
      compareStrings(str9, str10);
   }

   public static void compareStrings(String str1, String str2) {
      int comparedResult = str1.compareTo(str2);
      if (comparedResult > 0) {
         System.out.println(str1 + " comes after " + str2);
      } else if (comparedResult < 0) {
         System.out.println(str1 + " comes before " + str2);
      } else {
         System.out.println(str1 + " is equal to " + str2);
      }
   }
}

Output:-

java comes after Java
programming comes after Programming
language comes after Language
Know comes before Program
Coding comes before program

public class Main {

   public static void main(String args[]) {
      String str1 = "java";
      String str2 = "Java";
      System.out.println(compareStrings(str1, str2));

      String str3 = "Programming";
      String str4 = "Programming";
      System.out.println(compareStrings(str3, str4));
   }

   public static boolean compareStrings(String str1, String str2) {
      if (str1.compareTo(str2) == 0) {
         return true;
      } 
      return false;
   }
}

Output:-

false
true

Java Compare Strings Alphabetically using equals()

In String class equals() method is overridden for content comparison, and it also compares strings alphabetically character by character. It returns true if both strings are equal else it returns false.

Java Alphabetically Compare Two Strings using equals() Method

public class Main {

   public static void main(String args[]) {
      String str1 = "java";
      String str2 = "Java";
      System.out.println(str1.equals(str2));

      String str3 = "Programming";
      String str4 = "Programming";
      System.out.println(str3.equals(str4));
   }
}

Output:-

false
true

Java Compare Strings Alphabetically

This is the traditional way to compare strings alphabetically in Java by creating a method of our own.

public class Main {

   public static void main(String[] args) {

      String str1 = "one";
      String str2 = "two";
      int value1 = compareStrings(str1, str2);
      getComparison(value1, str1, str2);

      String str3 = "three";
      String str4 = "four";
      int value2 = compareStrings(str3, str4);
      getComparison(value2, str3, str4);

      int value3 = compareStrings(str4, str4);
      getComparison(value3, str4, str4);
   }

   public static int compareStrings(String str1, String str2) {
      for (int i = 0; i < str1.length() && i < str2.length(); i++) {
         if ((int) str1.charAt(i) == (int) str2.charAt(i)) {
            continue;
         } else {
            return (int) str1.charAt(i) - (int) str2.charAt(i);
         }
      }

      if (str1.length() < str2.length()) {
         return (str1.length() - str2.length());
      } else if (str1.length() > str2.length()) {
         return (str1.length() - str2.length());
      } else {
         return 0;
      }
   }

   private static void getComparison(int value, String str1, 
                                     String str2) {
      if (value > 0) {
         System.out.println("\"" + str1 + "\" comes after \"" 
                            + str2 + "\"");
      } else if (value < 0) {
         System.out.println("\"" + str1 + "\" comes before \"" 
                            + str2 + "\"");
      } else {
         System.out.println("\"" + str1 + "\" and \"" + str2 
                            + "\" are equal");
      }
   }
}

Output:-

“one” comes before “two”
“three” comes after “four”
“four” and “four” are equal

In the above program while displaying the strings we have used escape sequence to print double quote around the string. See more:- Escape Sequence In Java

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.