
How to compare two strings in java?

package Compare;
public class StringTest {
public static void main(String [] args){
String str="Roseindia";
String str1="Roseindia";
if (str.compareTo(str1) == 0)
{
System.out.println("The two strings are the same.");
}
}
}
Output:
The two strings are the same.
Description:-Here is an example of comparing two strings. By using the method compareTo, we have compared strings. If the strings are exactly the same, the compareTo method will return a value of 0 (zero).
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.