在 Java 中,何时使用 StringUtils.containsIgnoreCase 与 equalsIgnoreCase?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11995163/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 07:12:26  来源:igfitidea点击:

In java, when to use StringUtils.containsIgnoreCase vs. equalsIgnoreCase?

javaequalscontains

提问by Abs0lute_Zer0

I'm doing some coding in java and I'm curious as to when to use StringUtils.containsIgnoreCase vs. equalsIgnoreCase? When is it more appropriate to use one over the other? What is actually the big difference? Thanks guys...any help is greatly appreciated. :)

我正在用 Java 编写一些代码,我很好奇什么时候使用 StringUtils.containsIgnoreCase 和 equalsIgnoreCase?什么时候使用一个比另一个更合适?实际上最大的区别是什么?谢谢你们……非常感谢任何帮助。:)

回答by Keppil

If you read the specs, you see that StringUtils.containsIgnoreCase()checks if a String contains another String while StringUtils.equalsIgnoreCase()checks if two Strings are equal.

如果您阅读规范,您会看到 StringUtils.containsIgnoreCase()检查一个字符串是否包含另一个字符串,同时StringUtils.equalsIgnoreCase()检查两个字符串是否相等。

回答by Roddy of the Frozen Peas

If you have the following Strings:

如果您有以下字符串:

String a = "ABCdefGHIjkl";
String b = "ABCDEFGHIJKL";
String c = "ABCd";

Then acontains c, but is not equal to c. acontains bandis equal to b. (Here where I say "equals", I mean "equalsIgnoreCase"; same for contains.)

然后a包含c,但不等于ca包含b并且等于b。(这里我说“等于”,我的意思是“equalsIgnoreCase”;对于包含也是如此。)

You'd use one or the other depending on whether you'd want partial matches to be accepted.

根据您是否希望接受部分匹配,您可以使用一种或另一种。

回答by Ajay George

StringUtils.containsIgnoreCase

is from Apache Commons. It checks for whether the string contains a search string.

来自 Apache Commons。它检查字符串是否包含搜索字符串。

 StringUtils.contains(null, *) = false
 StringUtils.contains(*, null) = false
 StringUtils.contains("", "") = true
 StringUtils.contains("abc", "") = true
 StringUtils.contains("abc", "a") = true
 StringUtils.contains("abc", "z") = false
 StringUtils.contains("abc", "A") = true
 StringUtils.contains("abc", "Z") = false

API docs for both methods: equalsIgnoreCaseand containsIgnoreCase

两种方法的 API 文档:equalsIgnoreCasecontainsIgnoreCase

回答by Chander Shivdasani

StringUtils.containsIgnoreCase: checks whether a particular String contains another String.

StringUtils.containsIgnoreCase: 检查特定字符串是否包含另一个字符串。

For example:

例如:

StringUtils.contains(null, *) = false
StringUtils.contains("abc", "") = true

equalsIgnoreCase: Checks if two Strings are the same.

equalsIgnoreCase: 检查两个字符串是否相同。

For example:

例如:

"Test".equalsIgnoreCase("Test") = true
"Test".equalsIgnoreCase("T") = false