String.Equals() with Multiple Conditions (And One Action on Result)

String.equals() with multiple conditions (and one action on result)

Possibilities:

  • Use String.equals():

    if (some_string.equals("john") ||
    some_string.equals("mary") ||
    some_string.equals("peter"))
    {
    }
  • Use a regular expression:

    if (some_string.matches("john|mary|peter"))
    {
    }
  • Store a list of strings to be matched against in a Collection and search the collection:

    Set<String> names = new HashSet<String>();
    names.add("john");
    names.add("mary");
    names.add("peter");

    if (names.contains(some_string))
    {
    }

Compare one String with multiple values in one expression

I found the better solution. This can be achieved through RegEx:

if (str.matches("val1|val2|val3")) {
// remaining code
}

For case insensitive matching:

if (str.matches("(?i)val1|val2|val3")) {
// remaining code
}

How to compare multiple strings?

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".

Second, split it up so you are performing boolean logic on the comparison results:

else if(!(question.equals("a") || question.equals("b")) {

How do I combine multiple if (stringname.equals(value))?

You have to use logical AND, which is && in Java, to make the if block code executed only if all the conditions are true, like:

if (wppanswer.equals("Yes") &&
tbanswer.equals("Yes") &&
ctanswer.equals("Yes") &&
ssowpanswer.equals("Yes") &&
ssowpanswer.equals("N/A") &&
cppanswer.equals("Yes")){
//your code here
}

Furthermore, as it's yet said in comments, it is better to use "Yes".equals(wppanswer) style in your code, to prevent the NullPointerException if your wppanswer or any other object will be NULL, while you call it's equals() method.

How to make it shorter - Java String.equal(string) method

You could create a function with variable number of arguments:

static bool compareMultiStrings(String words, String ... stringi) {
if (words == null) return false;

for(String s : stringi){
if (words.equals(s)) return true;
}

return false;
}

and then call it from your if:

if (
compareMultiStrings(
"WORDS",
object.string1, object.string2, object.string3, object.string4,
object.string5, object.string6, object.string7, object.string8,
object.string9)
)
{ ..... }

The first argument if for your constants string, "WORDS". The other parameters is the varargs parameter.

multiple conditions for JavaScript .includes() method

That should work even if one, and only one of the conditions is true :

var str = "bonjour le monde vive le javascript";
var arr = ['bonjour','europe', 'c++'];

function contains(target, pattern){
var value = 0;
pattern.forEach(function(word){
value = value + target.includes(word);
});
return (value === 1)
}

console.log(contains(str, arr));

I need to Write OR statement within a string

You can't do that as you want it from our example. You have to do it like so:

if(myFunction.equals(firstString) || myFunction.equals(secondString))

How to compare multiple strings inside an if statement?

You cannot compare a variable against multiple values like that in C++. You should be doing:

if (theString == "Seven" || theString == "seven" || theString ==  "7")
{
theInt = 7;
cout << "You chose: " << theInt << endl;
}
else if (theString == "Six" || theString == "six" || theString == "6")
{
theInt = 6;
cout << "You chose: " << theInt << endl;
}

How to code logical expression 'equals MR or X '?

This should do the trick because it applies the logical "or" to two conditions, instead of one condition and one bare string.

if (l.getPlot().equals("MR")) || (l.getPlot().equals("X")){

Here is an improved version (credits Tim Biegeleisen), which avoids a null pointer exception.

if ("MR".equals(l.getPlot()) || "X".equals(l.getPlot()))

If the argument to equals() evaluates to NULL, the result is a clean false.

How can I open up my equals statements to accept more than one parameter in Java?

Is this possible in Java?

No.

The || operator takes two boolean operands. There's no way to change this. Java does not allow you to overload operators, or to "extend the language syntax" in other ways.

The closest you could get would be to define a overload for equals like this:

public boolean equals(Object ... others) {
for (Object other: others) {
if (this.equals(other)) {
return true;
}
}
return false;
}

and use it like this:

if (aLongVariableName.equals(classInstance.aDifferentProperty,
classInstance.anotherProperty)) {
...
}

... which is not really close at all. (And a pretty bad idea as well, IMO)

In fact, I can't think of any language that supports a syntax analogous to what you are proposing. It strikes me that there is too much potential for ambiguity for this to be a sound construct.



Related Topics



Leave a reply



Submit