matches: Will check if the complete string entered is equal to the value present in the string object.
equalsIgnoreCase: Ignoring the case, it checks if the string entered is equal to the value present in the string object.
equals: Case sensitive and it checks if the string entered is equal to the value present in the string object.
This is what I know about the methods, present in String class.
Are there any other differences(Am I missing any valuable differences)?
If there are no differences, then why cant matches method be removed from String class, since the functionality it puts forth can be achieved using the other above mentioned methods, appropriately.
There is a big difference - matches checks the match of a
String
to a regular expression pattern, not the same string. Do not be mislead by the fact that it receives aString
as an argument.For example:
matches
returns true if the string matches a regular expression, therefore, it should not be removed from the String class.This is what I got from the documentation...
matches (
String regex
): Tells whether or not this string matches the given regular expressionequals (
String Object
): 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.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.The key difference is that
matches
matches a regular expressions whereas equals matches a specific String.I suggest you have a look at what a regular expression is
matches() used for verifying---- whether the given string matches to specified regexpression
ex.;String s = "humbapumpa jim"; assertTrue(s.matches(".(jim|joe)."));
equals() for just checking the given string with specified string as exact match. equalsIgnoreCase() --- will ignore the case sensitive.