Program
public class StringEqualsAndIgnoreCase
{
public static void main(String[] args)
{
String s1 = new String(“welcome”);
String s2 = s1;
System.out.println(s1.equals(s2));
System.out.println(s1);
System.out.println(s2);
System.out.println(s1.equalsIgnoreCase(s2));
}
}
Output
true welcome welcome true
equals
Description
public boolean equals(Object anObject)
Compares this string to the specified object. The result is true if and only if the argument is not null and is a String object that represents the same sequence of characters as this object.
For finer-grained String comparison, refer to Collator.
Overrides:
equals in class Object
Parameters:
anObject – The object to compare this String against
Returns:
true if the given object represents a String equivalent to this string, false otherwise
equalsIgnoreCase
public boolean equalsIgnoreCase?(String anotherString)
Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.
Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:
The two characters are the same (as compared by the == operator)
Calling Character.toLowerCase(Character.toUpperCase(char)) on each character produces the same result.
Note that this method does not take locale into account, and will result in unsatisfactory results for certain locales. The Collator class provides a locale-sensitive comparison.
Parameters:
anotherString – The String to compare this String against
Returns:
true if the argument is not null and it represents an equivalent String ignoring case; false otherwise