This article will let you understand how to compare strings in java with the help of the below-listed procedures:
- How to Compare Strings Using equals() method.
- How to Compare Strings Using String.equalsIgnoreCase()
- How to Compare Strings Using compareTo() Method.
So, let’s start!
How to Compare Strings Using equals() method
The Java String class provides a build-in boolean method named equals() which is used to compare the values/content of two different strings. It will return true if the content of both strings is the same and return false if a mismatch occurs.
We can utilize the constructor of the Strings class to create a string as shown in the below snippet:
Example
In the below-given code snippet, we will create multiple strings and compare them using equals() method:
public static void main(String[] args) {
String str1 = "Welcome to Linuxhint.com";
String str2 = new String("Welcome to Linuxhint.com");
String str3 = "Linuxhint";
System.out.println(str1.equals(str2));
System.out.println(str1.equals(str3));
System.out.println(str2.equals(str3));
}
}
Firstly, we created three strings, then we utilized the equals() method to make string comparison. We compared the first string with the second string, the first string with the third string, and finally, we compared the second string with the third string:
The above snippet verifies the working of the equals() method.
How to Compare Strings Using String.equalsIgnoreCase()
In java, the equals() is a case-sensitive method which means if two strings have the same content but differ in terms of the case (upper case, lower case), then the equals() method will return false. So, if we want to make a string comparison regardless of case-sensitivity then we can use the equalsIgnoreCase() method.
Example
In this example, we will use both equals() and equalsIgnoreCase() methods to provide you with better clarity of both these methods:
The above code will generate the following output:
The output verifies that the equalsIgnoreCase() method compares the strings regardless of their case and hence returns true, while the equals() method is a case-sensitive method hence returns false.
How to Compare Strings Using compareTo() Method
In Java, the String class provides another handy method named the compareTo() method for lexicographically(strings with the same length, same characters present at the same position) comparison. It returns zero if “string1 == string2”, it returns a positive number(difference of character values) if “string1 > string2”, and it returns a negative value if “string1 < string2”.
public static void main(String[] args) {
String str1 = "Welcome to Linuxhint.com";
String str2 = new String("Welcome to Linuxhint.com");
String str3 = "Linuxhint.com";
System.out.println(str1.compareTo(str2));
System.out.println(str1.compareTo(str3));
System.out.println(str3.compareTo(str2));
}
}
The above code-snippet will provide the following output:
“0” showed string1 is equal to string 2, 11 showed that string1 has (11) extra characters than string2, and -11 showed that string3 has (11) fewer characters than string2.
Conclusion
Numerous methods can be used in java to compare strings such as equals(), equalsIgnoreCase(), compareTo() etc. The equals() method compares the values/content of two different strings and returns true if the content of both strings is the same and returns false if a mismatch occurs, the equalsIgnoreCase() method makes strings comparison regardless of case sensitivity, and compareTo() method makes lexicographically comparison of the strings. This article explained various approaches for strings comparison in java and considered multiple examples for better understanding.