Java 字符串类中matches和equalsIgnoreCase或equals的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9700115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Difference between matches and equalsIgnoreCase or equals in string class
提问by Anuj Balan
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.
equalsIgnoreCase:忽略大小写,它检查输入的字符串是否等于字符串对象中存在的值。
equals: Case sensitive and it checks if the string entered is equal to the value present in the string object.
equals:区分大小写,并检查输入的字符串是否等于字符串对象中存在的值。
This is what I know about the methods, present in String class.
这就是我对 String 类中存在的方法的了解。
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.
如果没有区别,那么为什么不能从 String 类中删除匹配方法,因为它提出的功能可以使用上述其他方法适当地实现。
采纳答案by MByD
There is a big difference - matcheschecks 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.
有一个很大的区别 -匹配检查 aString
与正则表达式模式的匹配,而不是同一个字符串。不要被它接收 aString
作为参数的事实误导。
For example:
例如:
"hello".equals(".*e.*"); // false
"hello".matches(".*e.*"); // true
回答by Peter Lawrey
The key difference is that matches
matches a regular expressions whereas equals matches a specific String.
主要区别在于matches
匹配正则表达式而等于匹配特定字符串。
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 expressionis
我建议你看看正则表达式是什么
回答by Terry Gardner
matches
returns true if the string matches a regular expression, therefore, it should not be removed from the String class.
matches
如果字符串与正则表达式匹配,则返回 true,因此,不应将其从 String 类中删除。
回答by Shashank Kadne
This is what I got from the documentation...
这是我从文档中得到的...
matches(String regex
): Tells whether or not this string matches the given regular expression
匹配(String regex
):判断这个字符串是否匹配给定的正则表达式
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.
equals( String Object
):将此字符串与指定对象进行比较。当且仅当参数不为 null 并且是表示与此对象相同的字符序列的 String 对象时,结果才为真。
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.
equalsIgnoreCase( String anotherString
):将此字符串与另一个字符串进行比较,忽略大小写。如果两个字符串的长度相同并且两个字符串中对应的字符是相等的忽略大小写,则认为两个字符串是相等的忽略大小写。
回答by mohan.reddy8
matches() used for verifying---- whether the given string matches to specified regexpression
matches() 用于验证----给定的字符串是否与指定的正则表达式匹配
ex.;String s = "humbapumpa jim"; assertTrue(s.matches(".(jim|joe)."));
例如;字符串 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.
equals() 只检查给定字符串与指定字符串作为精确匹配。equalsIgnoreCase() --- 将忽略大小写敏感。