1. Use the equals () method to compare whether two strings are equal. It has the following general form:
Boolean equals (Object Str)
Here, STR is a string object used to compare with the call String object. If the two strings have the same characters and length, true is returned; otherwise, false is returned. This comparison is case sensitive.
2. To perform case-insensitive comparison, you can call the equalsignorecase () method. When comparing two strings, it considers the A-Z and A-Z are the same. The general form is as follows:
Boolean inclusignorecase (string Str)
Here, STR is a string object used to compare with the call String object. If the two strings have the same characters and length, true is returned. Otherwise, false is returned. The following example illustrates the equals () and equalsignorecase () methods:
// Demonstrate equals () and inclusignorecase ().
Class sdemo {
Public static void main (string ARGs []) {
String S1 = "hello ";
String S2 = "hello ";
String S3 = "good-bye ";
String S4 = "hello ";
System. Out. println (S1 + "equals" + S2 + "->" +
S1.equals (S2 ));
System. Out. println (S1 + "equals" + S3 + "->" +
S1.equals (S3 ));
System. Out. println (S1 + "equals" + S4 + "->" +
S1.equals (S4 ));
System. Out. println (S1 + "equalsignorecase" + S4 + "->" +
S1.equalsignorecase (S4 ));
}
}
The program output is as follows:
Hello equals hello-> true
Hello equals good-bye-> false
Hello equals hello-> false
Hello equalsignorecase hello-> true