Java 如何比较多个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20085287/
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 compare multiple strings?
提问by SaraMaeBee
I was wondering how I can compare multiple strings in one line. I tried using the || but it doesn't work for booleans or strings. this is what my code is like:
我想知道如何在一行中比较多个字符串。我尝试使用 || 但它不适用于布尔值或字符串。这就是我的代码:
}else if(question != "a" || "b") {
System.out.println("Sorry that isn't an A or a B");
For those who marked it duplicate, I checked over 200 questions here on stack overflow, and none worked. The one @Chrylis posted actually didn't help. they were just asking about the difference in == and .equals()
对于那些将其标记为重复的人,我在这里检查了 200 多个关于堆栈溢出的问题,但没有一个奏效。@Chrylis 发布的那个实际上没有帮助。他们只是在问 == 和 .equals() 的区别
采纳答案by nanofarad
First of all, don't use ==
for strings. You'll learn why later. You want to compare strings by their contents, not where they are in memory. In rare cases a string of "a"
could compare false to another string called "a"
.
首先,不要==
用于字符串。稍后您将了解原因。您想通过内容来比较字符串,而不是它们在内存中的位置。在极少数情况下,一个字符串"a"
可能会与另一个名为"a"
.
Second, split it up so you are performing boolean logic on the comparison results:
其次,将其拆分,以便对比较结果执行布尔逻辑:
else if(!(question.equals("a") || question.equals("b")) {
回答by arshajii
You can try using Arrays.asList()
:
您可以尝试使用Arrays.asList()
:
else if (!Arrays.asList("a", "b").contains(question)) {
...
}
回答by dkatzel
As an aside, you should use equals
for objects not ==
顺便说一句,您应该将equals
对象用于不==
To answer your question, you have to repeat the equals call on both sides of the ||
要回答你的问题,你必须在两边重复 equals 调用 ||
}else if( ! (question.equals("a") || question.equals("b")) ) {
回答by rgettman
Two things wrong: You can't just specify multiple values with ||
(or &&
) like that. You need to specify both the left side and the right side explicitly each time.
有两点错误:您不能像那样使用||
(或&&
)指定多个值。每次都需要明确指定左侧和右侧。
Second, use equals
to compare String
values, not the ==
(or in this case !=
) operators. ==
compares two object references to see if they are the same object.
其次,用于equals
比较String
值,而不是==
(或在这种情况下!=
)运算符。 ==
比较两个对象引用,看它们是否是同一个对象。
} else if (!("a".equals(question) || "b".equals(question)))
Or an alternative is to make a temporary List
and use contains
, which might be clearer for longer lists of things to test:
或者另一种方法是制作一个临时的List
并使用contains
,这对于更长的要测试的事物列表可能更清晰:
} else if (!Arrays.asList("a", "b").contains(question))
回答by Michael Yaworski
}else if( !(question.equals("a") || question.equals("b")) {
System.out.println("Sorry that isn't an A or a B");
You can't do NOT equals a OR b
You have to do NOT(equals a OR equals b)
你不能做NOT equals a OR b
你必须做NOT(equals a OR equals b)
Secondly, you are comparing strings with !=
, but you should be comparing strings using the .equals(String)
method. This has been said millions of times, but: ==
and !=
are comparing object references, whereas .equals(String)
is comparing String values.
其次,您正在使用 比较字符串!=
,但您应该使用 比较字符串。equals(String)
方法。这已经说过数百万次了,但是:==
并且!=
正在比较对象引用,而.equals(String)
正在比较 String值。
回答by Stefan Haustein
String[] options = {"a", "b"}; // Must be sorted.
if (java.util.Arrays.binarySearch(options, question) < 0) {
System.out.println("Sorry that isn't an A or a B");
}
Alternatively (assuming your strings don't contain |
:
或者(假设您的字符串不包含|
:
if ("a|b".indexOf(question) == -1) {
System.out.println("Sorry that isn't an A or a B");
}