Difference between matches and equalsIgnoreCase or equals in string class

后端 未结 5 793
一整个雨季
一整个雨季 2020-12-29 09:44

matches: Will check if the complete string entered is equal to the value present in the string object.

equalsIgnoreCase: Ignoring t

相关标签:
5条回答
  • 2020-12-29 10:03

    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 a String as an argument.

    For example:

    "hello".equals(".*e.*"); // false
    "hello".matches(".*e.*"); // true
    
    0 讨论(0)
  • 2020-12-29 10:03

    matches returns true if the string matches a regular expression, therefore, it should not be removed from the String class.

    0 讨论(0)
  • 2020-12-29 10:07

    The key difference is that matches matches a regular expressions whereas equals matches a specific String.

    System.out.println("hello".matches(".+"));    // Output: true
    System.out.println("hello".equals(".+"));     // Output: false
    System.out.println("wtf?".matches("wtf?"));   // Output: false
    System.out.println("wtf?".equals("wtf?"));    // Output: true
    

    I suggest you have a look at what a regular expression is

    0 讨论(0)
  • 2020-12-29 10:19

    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.

    0 讨论(0)
  • 2020-12-29 10:28

    This is what I got from the documentation...

    matches (String regex): Tells whether or not this string matches the given regular expression

    equals (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.

    0 讨论(0)
提交回复
热议问题