String Manipulation can be of immense help in different domains. It may help in text analytics, data matching, data mining etc. In this article we will focus on comparing two strings in Java which again different purposes for string manipulation. Following are the pointers that would be discussed here
- String Equals Method
- String Equals Ignore Case
- Object Equals Method
- String Compare To Method
- Using Double Equal To Operator
So let us get started then,
A sequence of characters can be defined as a String. They are immutable, i.e. they cannot be modified once created. There are various methods to compare two strings in java, as seen below.
String Equals Method
The strings are compared on the basis of the values present in the string. The method returns true if the values of the two strings are same, and false, if the values don’t match.
public class Main { public static void main[String args[]] { String str1 = new String["Rock"]; String str2 = new String["Roll"]; String str3 = new String["rock"]; String str4 = new String["Rock"]; String str5 = new String["Roll"]; //comparing the strings System.out.println["Comparing " + str1 + " and " + str2 + " : " + str1.equals[str2]]; System.out.println["Comparing " + str3 + " and " + str4 + " : " + str3.equals[str4]]; System.out.println["Comparing " + str4 + " and " + str5 + " : " + str4.equals[str5]]; System.out.println["Comparing " + str1 + " and " + str4 + " : " + str1.equals[str4]]; } }
Output:
Comparing Rock and Roll :false
Comparing rock and Rock :false
Comparing Rock and Roll :false
Comparing Rock and Rock :true
Let us continue with the second bit of this article,
String Equals Ignore Case
This method compares the two strings, and does not take into account the case of the string[ lower or upper]. Returns true if the values are equal and not null.
public class Main { public static void main[String args[]] { String str1 = new String["Rock"]; String str2 = new String["Roll"]; String str3 = new String["rock"]; String str4 = new String["Rock"]; String str5 = new String["Roll"]; //Comparing Strings System.out.println["Comparing " + str1 + " and " + str2 + " : " + str1.equalsIgnoreCase[str2]]; System.out.println["Comparing " + str3 + " and " + str4 + " : " + str3.equalsIgnoreCase[str4]]; System.out.println["Comparing " + str4 + " and " + str5 + " : " + str4.equalsIgnoreCase[str5]]; System.out.println["Comparing " + str1 + " and " + str4 + " : " + str1.equalsIgnoreCase[str4]]; } }
Output:
Comparing Rock and Roll :false
Comparing rock and Rock :true
Comparing Rock and Roll :false
Comparing Rock and Rock :true
Let us move further with the next bit of this comparing two strings in Java article,
Object Equals Method
If the arguments are equal to the each other, the method returns true, else, it returns false. If both the arguments present are null, the output returned is true. If a single argument is of a null value, the output returned is false.
import java.util.*; public class Main { public static void main[String args[]] { String str1 = new String["Rock"]; String str2 = new String["Roll"]; String str3 = new String["Roll"]; String str4 = null; String str5 = null; System.out.println["Comparing " + str1 + " and " + str2 + " : " + Objects.equals[str1, str2]]; System.out.println["Comparing " + str2 + " and " + str3 + " : " + Objects.equals[str2, str3]]; System.out.println["Comparing " + str1 + " and " + str4 + " : " + Objects.equals[str1, str4]]; System.out.println["Comparing " + str4 + " and " + str5 + " : " + Objects.equals[str4, str5]]; } }
Output:
Comparing Rock and Roll :false
Comparing Roll and Roll :true
Comparing Rock and null :false
Comparing null and null :true
Let us move further now
String Compare To Method
In this method, the input strings are compared to each other. The value returned after comparison is as follows:
- if[str1>str2], a positive value is returned.
- If[str1==str2], 0 is returned.
- If[str1