Home > Java > javaTutorial > Compare two Strings in Java

Compare two Strings in Java

PHPz
Release: 2024-08-30 15:34:47
Original
651 people have browsed it

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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.");
}
Copy after login

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.");
}
Copy after login

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.");
}
Copy after login

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.");
}
}
}
Copy after login

Output

Compare two Strings in Java

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.");
}
}
}
Copy after login

Output

Compare two Strings in Java

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.");
}
}
}
Copy after login

Output

Compare two Strings in Java

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.");
}
}
}
Copy after login

Output

Compare two Strings in Java

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.");
}
}
}
Copy after login

Output

Compare two Strings in Java

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.");
}
}
}
Copy after login

Output

Compare two Strings in Java

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.

The above is the detailed content of Compare two Strings in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template