w3resource

Java: Compare two strings lexicographically, ignoring case differences


Write a Java program to compare two strings lexicographically, ignoring case differences.

Visual Presentation:

Java String Exercises: Compare two strings lexicographically, ignoring case differences

Sample Solution:

Java Code:

// Define a public class named Exercise6.
public class Exercise6 {
    // Define the main method.
    public static void main(String[] args) {
        // Declare and initialize two string variables, str1 and str2.
        String str1 = "This is exercise 1";
        String str2 = "This is Exercise 1";
        
        // Print the first string.
        System.out.println("String 1: " + str1);
        // Print the second string.
        System.out.println("String 2: " + str2); 
       
        // Compare the two strings ignoring case sensitivity and get the result of the comparison.
        int result = str1.compareToIgnoreCase(str2);

        // Display the results of the comparison.
        if (result < 0) {
            // If str1 (ignoring case) is less than str2 (ignoring case).
            System.out.println("\"" + str1 + "\"" +
                " is less than " +
                "\"" + str2 + "\"");
        } else if (result == 0) {
            // If str1 (ignoring case) is equal to str2 (ignoring case).
            System.out.println("\"" + str1 + "\"" +
                " is equal to " +
                "\"" + str2 + "\"");
        } else { // if (result > 0)
            // If str1 (ignoring case) is greater than str2 (ignoring case).
            System.out.println("\"" + str1 + "\"" +
                " is greater than " +
                "\"" + str2 + "\"");
        }
    }
}

Sample Output:

String 1: This is exercise 1                                                                                  
String 2: This is Exercise 1                                                                                  
"This is exercise 1" is equal to "This is Exercise 1"

Flowchart:

Flowchart: Java String  Exercises - Compare two strings lexicographically, ignoring case differences

For more Practice: Solve these Related Problems:

  • Write a Java program to compare two strings ignoring case and report if one string is a substring of the other.
  • Write a Java program that compares two strings in a case-insensitive manner and then sorts them alphabetically.
  • Write a Java program to perform a case-insensitive lexicographic comparison on string inputs from the user.
  • Write a Java program to check if two strings are lexicographically equal when both are converted to lowercase.

Java Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a Java program to compare two strings lexicographically. Two strings are lexicographically equal if they are the same length and contain the same characters in the same positions.
Next: Write a Java program to concatenate a given string to the end of another string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.