String comparisons are common things for any programming language. There are various ways we can compare strings. In Java itself, there are various ways we can check for string equals. We have a list of functions; we can achieve this by using that in Java programming language. In general, we have Java equals() and equalsIgnoreCase() for check the string equality. The equals() is a function that is a case-sensitive function. If we want to move ahead with the in-case sensitive we can use the equalsIgnoreCase() function to do the same job. We can also check for the string comparison by using the == operator and compareTo().
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
For using the string equals we must have at least two strings. Let’s say STR_1 and STR_2. We can simply use the Java equals operator.
== Operator
String STR_1 = "This is String"; String STR_2 = "This is String"; if(STR_1 == STR_2){ System.out.print("Both strings are equal."); }
After running the above code, we can see both strings are equals.
equals() function – Again, this function is a case-sensitive for checking whether two strings are equals or not. This works the way compareTo() works for strings. The return type of this function is Boolean. This is a case-sensitive function.
String str1 = "Hello World"; String str2 = "Hello World"; boolean equalsStr = str1.equals(str2);
equalsIgnoreCase() function – This function working is very similar to the compareToIgnoreCase() function. This ignore the case checking in addition to the equals() function workings.
String str1 = "Hello World"; String str2 = "Hello World"; boolean equalsStr = str1.equalsIgnoreCase(str2);
Java compareTo() function – This is a case sensitive function for string comparison. If we would like to compare the same text or the string with the case checking, this function is best to do so. In the below code we have the same string ‘Hello World’ but in the str2 we have ‘Hello’ word with small ‘h’. So these two string are not equal.
String str1 = "Hello World"; String str2 = "hello World"; int compare = str1.compareTo(str2);
We will not 0 in compare identifier if both strings are equal. But in the above case we will not get the 0. In other words, the strings are not equal for str1 and str2.
Java compareToIgnoreCase() function – In case we want the string to be compared with the case matching then this function will be helpful for us. Let’s see the same strings as mentioned above using this function compareToIgnoreCase().
String str1 = "Hello World"; String str2 = "hello World"; int compare = str1.compareToIgnoreCase(str2);
int compareTo(String str)
The return type of this function is an integer. If both strings will be equal, then this function will return the 0. This will also check for the strings are in the same case. Strings will be considered as not equal even after the equal, but the case is not in a similar order.
int compareToIgnoreCase(String str)
The return type of this function is an integer. If both strings are equal, then this function will return the 0. This will also check for the strings are in the same case. This will ignore the case differences.
boolean equals (String str1); If strings will be equal, then it will return true otherwise false. boolean equalsIgnoreCase(String str1);
If strings will be equal, then it will return the true. Case differences will be ignored by this function.
Now, it’s time to see some examples to understand what we have discussed until now.
Code:
public class StrEquals { public static void main(String[] args) { String str1 = "Hello World"; String str2 = "hello World"; boolean equalsStr = str1.equals(str2); if(equalsStr == true){ System.out.print("Strings are equal."); }else{ System.out.print("Strings are not equal."); } } }
Output
This is an in-case sensitive function.
Code:
public class StrEquals { public static void main(String[] args) { String str1 = "Hello World"; String str2 = "hello World"; boolean equalsStr = str1.equalsIgnoreCase(str2); if(equalsStr == true){ System.out.print("Strings are equal."); }else{ System.out.print("Strings are not equal."); } } }
Output
Code:
public class StrEquals { public static void main(String[] args) { String str1 = "Hello World"; String str2 = "hello World"; int equalsStr = str1.compareTo(str2); if(equalsStr == 0){ System.out.print("Strings are equal."); }else{ System.out.print("Strings are not equal."); } } }
Output
This is an in-case sensitive function.
Code:
public class StrEquals { public static void main(String[] args) { String str1 = "Hello World"; String str2 = "hello World"; int equalsStr = str1.compareToIgnoreCase(str2); if(equalsStr == 0){ System.out.print("Strings are equal."); }else{ System.out.print("Strings are not equal."); } } }
Output
Yes, we can use the == for the string comparison. This will work as a case sensitive equal check. If we want to ignore the case checking then the toLowerCase() and toUpperCase() can do the job for us. By using any of this function, the string will be in the same case then we can apply this == for the string equals.
Code:
public class StrEquals { public static void main(String[] args) { String string1 = "Hello World, this is first."; String string2 = "hello World, this is first."; if(string1 == string2){ System.out.print("Strings are equal."); }else{ System.out.print("Strings are not equal."); } } }
Output
Now, we have covered all the examples.
We case use any built-in functions are per our business requirements. We have equals(), equalsIgnoreCase(), compareTo(), compareToIgnoreCase etc built-in functions in Java to handle the string comparison. A developer or the coder should be smart enough to move ahead with any of this function as per the business requirements. If our need is not that much of critical, we can also go for the traditional approach of the string comparison that is == (double equals) operator itself.
The above is the detailed content of Java String Equals. For more information, please follow other related articles on the PHP Chinese website!