Updated April 11, 2023
Introduction to Compare Two Strings in Java
Comparing strings in any programming language is very common. There are various ways we can achieve string comparison in the Java Programming language. We can do a comparison of two strings in various ways, either by using the built-in function or the custom code. Functions like compareTo(), compareToIgnoreCase() and == can be used for the string comparison purpose in the Java. There are a few built-in functions we can use to compare not just the strings but also the objects. We can use compareTo() and compareToIgnoreCase() not just for string comparison but also for the object comparison. We will see various examples of the string comparison in the coming sections. In this topic, we are going to learn about Compare two Strings in Java.
How Does it Work?
We will have two strings, let’s say string1 and the string2. We can compare this string using the various available mediums in the Java programming language.
Using = operator,
String string1 = "Hello";
String string2 = "Hello";
if(string1 == string2){
System.out.print("Both strings are equal.");
}
the output we can see will as Both strings are equal.
In the same way, we can use the compareTo() as a case-sensitive function, and if we want to ignore the case comparison, we can use the compareToIgnoreCase() function.
Using the compareTo()
We can use this function without importing any additional library. For this, we will have two strings. Let’s see the example code.
String string1 = "Hello World";
String string2 = "hello World";
int compare = string1.compareTo(string2);
if(compare == 0){
System.out.print("Strings are equal.");
}else{
System.out.print("Strings are not equal.");
}
This will give ‘Strings are not equal.’ as an output because strings are the same, but the cases are different.
Using the compareToIgnoreCase()
This function works the same as compare(). This will be ignored if the case is different.
String string1 = "Hello World - 1";
String string2 = "hello World - 1";
int compare = string1.compareToIgnoreCase(string2);
if(compare == 0){
System.out.print("Strings are equal.");
}else{
System.out.print("Strings are not equal.");
}
The output of the above function will be ‘Strings are equal.’ because we are using the compareToIgnoreCase() function. We should use the above-mentioned techniques as per our requirements.
Syntax of Compare two Strings in Java
Here is the syntax mention below
1. int compareTo(String str)
This function can be used to compare two strings. The return type of this function is an integer. It returns 0 if strings are equal. compareTo is the built-in function itself in the java. str is the string we will pass as an argument. But this will work if both cases are the same.
2. int compareTo(Object obj)
In the same way, we can use the object to compare using the compareTo() function.
3. int compareToIgnoreCase(String str)
This function remains the same as compareTo(), but it will not check the case of the given strings in-between the process.
4. int compareToIgnoreCase(Object obj)
This function can be used to compare the two strings.
Examples of Compare two Strings in Java
Here are the examples mention below
Example #1 Using = operator
This is one of the simplest ones we can use for string comparison. This is a case-sensitive way to compare two strings. If we want to work this for both, we can to pass the string after changing both in a specified case (lower or upper).
public class StringCompare {
public static void main(String[] args) {
String string1 = "Hello World";
String string2 = "Hello World";
if(string1 == string2){
System.out.print("Strings are equal.");
}
}
}
Output
Example #2 – Using = operator (ignore case)
We can see here in the example, strings are the same, but cases are different. So, we are changing the case to lowercase for both strings first then using the = operator to get the job done.
public class StringCompare {
public static void main(String[] args) {
String string1 = "Hello World";
String string2 = "hello World";
string1.toLowerCase();
string2.toLowerCase();
if(string1 == string2){
System.out.print("Strings are equal.");
}else{
System.out.print("Strings are not equal.");
}
}
}
Output
Example #3 – Using compareTo (case-sensitive)
Strings will have the same text, and the same case will be considered as the same using this function.
public class StringCompare {
public static void main(String[] args) {
String string1 = "This is string - Hello World";
String string2 = "This is string - Hello World";
int compare = string1.compareTo(string2);
if(compare == 0){
System.out.print("Strings are equal.");
}else{
System.out.print("Strings are not equal.");
}
}
}
Output
Example #4- Using compareTo (case-sensitive)
Strings are the same, but the case is different in the below example.
public class StringCompare {
public static void main(String[] args) {
String string1 = "This is String - Hello World";
String string2 = "This is string - Hello World";
int compare = string1.compareTo(string2);
if(compare == 0){
System.out.print("Strings are equal.");
}else{
System.out.print("Strings are not equal.");
}
}
}
Output
Example #5- Using compareToIgnoreCase (no case checking)
The below program will ignore the case checking while comparing two strings.
public class StringCompare {
public static void main(String[] args) {
String string1 = "This is string - Hello World";
String string2 = "This is string - Hello World";
int compare = string1.compareToIgnoreCase(string2);
if(compare == 0){
System.out.print("Strings are equal.");
}else{
System.out.print("Strings are not equal.");
}
}
}
Output
Example #6- Using compareToIgnoreCase (No case checking)
The below program will ignore the case checking while comparing two strings.
public class StringCompare {
public static void main(String[] args) {
String string1 = "This is String - Hello World-1";
String string2 = "this is string - Hello World-1";
int compare = string1.compareToIgnoreCase(string2);
if(compare == 0){
System.out.print("Strings are equal.");
}else{
System.out.print("Strings are not equal.");
}
}
}
Output
Conclusion
The Java programming language itself comes up with various features or built-in functions we can use in comparing two strings. We should use the built-in function until and unless we don’t have any specific needs. For comparing two strings, we can simply use the equals to the operator (=). A developer should be careful of using string comparison functions as string cases could be wrongful sometimes for any program or the project.
Recommended Articles
This is a guide to Compare two Strings in Java. Here we discuss the Examples of Compare two Strings in Java and How does it work along with the syntax. You may also have a look at the following articles to learn more –