There are many ways to compare two Strings in Java:
- Using == operator
- Using equals() method
- Using compareTo() method
- Using compareToIgnoreCase() method
- Using compare() method
- Method 1: using == operator
Double equals operator is used to compare two or more than two objects, If they are referring to the same object then return true, otherwise return false. String is immutable in java. When two or more objects are created without new keyword, then both object refer same value. Double equals operator actually compares objects references.
- Method 1: using == operator
- Below example illustrate the use of == for string comparison in Java:
A == A: true A == A: true A == A: true A == A: false
- Method 2: Using equals() method
In Java, string equals() method compares the two given strings based on the data / content of the string. If all the contents of both the strings are same then it returns true. If all characters are not matched then it returns false.
- Below example illustrate the use of .equals for string comparison in Java:
A .equals A: true A .equals a: false A .equals a: false A .equals A: true
- Method 3: Using compareTo() method
In java Comparable interface compares values and returns an int, these int values may be less than, equal, or greater than. The java compare two string is based on the Unicode value of each character in the strings. If two strings are different, then they have different characters at some index that is a valid index for both strings, or their lengths are different, or both.
Assuming index ‘i’ is where characters are different then compareTo() will return firstString.charAt(i)-secondString.charAt(i).
- Below example illustrate the use of .compareTo for string comparison in Java:
A .compareTo A: 0 A .compareTo a: -32 a .compareTo A: 32 A .compareTo A: 0
- Method 4: Using equalsIgnoreCase() method
Java String equalsIgnoreCase() method is much similar to equals() method, except that case is ignored like in above example String object s4 compare to s3 then equals() method return false, but here in case of equalsIgnoreCase() it will return true. Hence equalsIgnoreCase() method is Case Insensitive.
- Below example illustrate the use of .equalsIgnoreCase for string comparison in Java:
A .equalsIgnoreCase A: true A .equalsIgnoreCase a: true A .equalsIgnoreCase a: true A .equalsIgnoreCase A: true
- Method 5: Using compare() method
In Java for locale specific comparison, one should use Collator class which is in java.text package. The one most important feature of Collator class is the ability to define our own custom comparison rules.
- Below example illustrate the use of compare() method in Java to compare Strings:
A collator.compare A: 0 A collator.compare a: 1 a collator.compare A: -1 A collator.compare A: 0
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.