java java中如何对多个元素使用equalsIgnoreCase()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16810663/
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
How to use equalsIgnoreCase() for multiple elements in java
提问by PSR
if(string.equalsIgnoreCase("first") ||
string.equalsIgnoreCase("second") || string.equalsIgnoreCase("third"))
I need to use 10 ||
here (I have 10 strings to check).Is there any simple solution for this.
我需要在||
这里使用 10 (我有 10 个字符串要检查)。是否有任何简单的解决方案。
And i need to find which condition is satisfied.
我需要找到满足哪个条件。
Thanks in advance..
提前致谢..
回答by Bathsheba
You could use string.matches()
which takes a regular expression into which you can coerce first, second third etc:
您可以使用string.matches()
which 接受一个正则表达式,您可以将第一个、第二个、第三个等强制转换为:
if (string.matches("first|second|third"))
or, for case insensitivity:
或者,不区分大小写:
if (string.matches("(?i)first|second|third"))
Regular expressions are complex though so could be a performance issue.
正则表达式很复杂,但可能是性能问题。
回答by Djon
If you need to know which match is true, then use a switch (Java 7 only):
如果您需要知道哪个匹配是正确的,请使用开关(仅限 Java 7):
switch (string.toLowerCase())
{
case "first": doSomething();
break;
case "second": ...;
break;
default: ...;
}
回答by Matthias Meid
You might want to use a Set<String>
and check whether it containsthe desired String
.
您可能想要使用 aSet<String>
并检查它是否包含所需的String
.
TreeSet<String> values = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
values.put("first");
values.put("second");
values.put("third");
if (values.contains(string.toLower())) {
}
This works in logarithmic time(O(log n)
), whereas the ||
-approach is (theoretically in worst case) linear. For 10 elements it's hardly relevant though.
这适用于对数时间( O(log n)
),而||
- 方法(理论上在最坏的情况下)是线性的。对于 10 个元素,它几乎不相关。
Edit (credits go to Walter):
编辑(学分转到沃尔特):
The TreeSet
class uses the Comparator
only to determine equality, rather than using equals
method. You can therefore put mixed case strings into it. From the Class TreeSet documentation:
本TreeSet
类使用Comparator
唯一确定的平等,而不是使用equals
方法。因此,您可以将大小写混合的字符串放入其中。从类 TreeSet 文档:
Note that the ordering maintained by a set (whether or not an explicit comparator is provided) must be consistent with equals if it is to correctly implement the Set interface. (See Comparable or Comparator for a precise definition of consistent with equals.) This is so because the Set interface is defined in terms of the equals operation, but a TreeSet instance performs all element comparisons using its compareTo (or compare) method, so two elements that are deemed equal by this method are, from the standpoint of the set, equal. The behavior of a set is well-defined even if its ordering is inconsistent with equals; it just fails to obey the general contract of the Set interface.
请注意,如果要正确实现 Set 接口,则集合维护的排序(无论是否提供显式比较器)必须与 equals 一致。(请参阅 Comparable 或 Comparator 以获取与 equals 一致的精确定义。)这是因为 Set 接口是根据 equals 操作定义的,但是 TreeSet 实例使用它的 compareTo(或 compare)方法执行所有元素比较,所以两个被此方法视为相等的元素,从集合的角度来看,是相等的。集合的行为是明确定义的,即使它的顺序与 equals 不一致;它只是不遵守 Set 接口的一般约定。
回答by kelunik
you can use your own function:
您可以使用自己的功能:
equalsIgnoreCase("string", "first", "second");
equalsIgnoreCase("string", "first", "second");
public boolean equalsIgnoreCase(String needle, String... haystack) {
foreach(String s : haystack) {
if(needle.equalsIgnoreCase(s)) {
return true;
}
}
return false;
}
回答by djm.im
Here is Java 8 and streams solution
这是 Java 8 和流解决方案
String string = "...";
List<String> match = Arrays.asList("first", "second", "third", "...");
if (match.stream().anyMatch(string::equalsIgnoreCase)) {
// ...
}