4 ways to compare Strings in Java (Bonus: Lexicographical Compare)
Comparing two Strings is a very common project requirement. It can be utilized in multiple ways.
- The comparison can be done to verify that the output is as expected.
- It can be utilized to validate a test case.
- There can be a functionality which depends on a certain input, and we can write conditions on the basis of input given. We have to compare in order to verify the correct input and direct to correct condition.
So, we can say that String comparison is a very important functionality in Java.
Today we'll learn multiple ways to do String comparison.
1. Using Inbuilt function equals()
The java documentation says :
boolean java.lang.String.equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
Let's check out an example:
String s1 = "Newstring"; String s2 = "NewString"; String s3 = new String("Newstring");
System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3));
Output:
true
2. Using Inbuilt function equalsIgnoreCase()
The java documentation says:
boolean java.lang.String.equalsIgnoreCase(String anotherString)
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding Unicode code points in the two strings are equal ignoring case.
Let's check out an example:
String s1 = "Newstring"; String s2 = "NewString"; String s3 = new String("Newstring"); System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s1.equalsIgnoreCase(s3));
Output:
true
3. Using the comparison operator '=='
The comparison operation '==' is another way of comparison. Let's check out the functionality with an example.
String s1 = "Newstring"; String s2 = "NewString"; String s3 = "NewString"; String s4 = new String("Newstring");
System.out.println(s1==s2); System.out.println(s1==s3); System.out.println(s1==s4); System.out.println(s2==s3);
Output:
false
false
true
4. Lexicographically compare
As per java documentation:
int java.lang.String.compareTo(String anotherString)
Compares two strings lexicographically. The comparison is based on the Unicode value of each character inthe strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argument string. The result is a negative integer if this String object lexicographically precedes the argument string. The result is a positive integer if this String object lexicographically follows the argument string. The result is zero if the strings are equal; compareTo returns 0 exactly when the equals(Object) method would return true.
So basically:
- Strings are equal -> output is 0
- String 1 is bigger than String 2 -> Output is a Positive value
- String 1 is lesser than String 2 -> Output is a Negative value
Let's check out an example:
String s1 = "Newstring"; String s2 = "NewString"; String s3 = "NewString"; String s4 = new String("Newstring");
System.out.println(s1.compareTo(s2)); System.out.println(s1.compareTo(s4)); System.out.println(s2.compareTo(s3));
Output:
0
0
public class CompareStrings { public static void main(String[] args) { String s1 = "Newstring"; String s2 = "NewString"; String s3 = "NewString"; String s4 = new String("Newstring"); //Option 1 System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3)); //Option 2 System.out.println(s1.equalsIgnoreCase(s2)); System.out.println(s1.equalsIgnoreCase(s3)); //Option 3 System.out.println(s1==s2); System.out.println(s1==s3); System.out.println(s1==s4); System.out.println(s2==s3); //Option 4 System.out.println(s1.compareTo(s2)); System.out.println(s1.compareTo(s4)); System.out.println(s2.compareTo(s3)); } }
Thanks for Reading the Article. If you have reached this far, we hope that the article was useful to you! Please Like/Share/Follow us on Facebook, Twitter, Tumblr.
Comments
Post a Comment