java 你如何检查一个字符串是否不等于一个对象?

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

How do you check if a string is not equal to an object?

javastring

提问by Jainam Patel

I ave a code where I want to use != sign. But since I am using String, How do I not equals Sign. Hers is my code. So I want all this statement not to equal to each other so it can print Tie Game.

我有一个代码,我想在其中使用 != 符号。但是由于我使用的是 String,我如何不等于 Sign。她是我的代码。所以我希望所有这些语句彼此不相等,以便它可以打印 Tie Game。

    if (Array[0] == Array[currentPlayer] &&  Array [1] == 
    Array[currentPlayer] && !Array [2] == Array[currentPlayer])

The above code is when everything equals to each other. But I want this statements not to equal each other.

上面的代码是当一切都彼此相等时。但我希望这些陈述彼此不同。

Keep in mind that I have not used Int or Char, I am using String.

请记住,我没有使用 Int 或 Char,我使用的是 String。

回答by Brian

For string inequality, negate a call to the equals method using the !:

对于字符串不等式,使用以下方法否定对 equals 方法的调用!

String x = "ABC";
String y = "XYZ";
if(!x.equals(y)) {
    //do stuff
}

!can be used to negate ANY boolean expression, and String.equalsreturns a boolean.

!可用于否定任何布尔表达式,并String.equals返回一个布尔值。

回答by hitz

You can do something like:

您可以执行以下操作:

if (!Array[0].equals(Array[currentPlayer]) &&  !Array[1].equals(Array[currentPlayer])  
    && Array[2].equals(Array[currentPlayer]))

Use equals()if you want case sensitive match meaning it will look at case of string as well when matching. If you want case insensitive matching you can use equalsIgnoreCase()method in place of equals()

使用equals(),如果你想区分大小写的匹配意味着它会看串的情况下,也当匹配。如果您想要不区分大小写的匹配,您可以使用equalsIgnoreCase()method 代替equals()