Check if something equals either option quickly?

If I have something and want to check that it’s equal to either of many things is there a way to do so without using the or operator / rewriting the whole thing?

For example, let’s say I have a string and want to check if it’s equal to “left” or “right”, is there a way to do

if(inputString == [“left”, “right”])
{
do stuff
}

Currently i’m doing it like this

if(inputString == “left” || inputString == “right”)
{
do stuff
}

And it seems like there should be a better way.

“one of many things”

You can use a switch-case instead:

switch ( inputString ) {
  case "left":
    Debug.Log("Left");
    break; // this command sends execution to the switch block's closing brace, }
  case "right":
  case "RIGHT":
    Debug.Log("Right"); // this line executes if inputString is "right" OR "RIGHT"
    break;
  default: // this is what is chosen if none of the above cases are a match
    Debug.Log("Default"); 
}

I guess that counts as rewriting, but there’s not a built-in way to compare to one-of-many-things.

You could write a function that might look like:

function IsOneOf( check : Object, list : Object[] ) : boolean {
  for ( var item : Object in list ) {
    if ( check == item ) return true;
  }
  return false;
}

You could use System.Linq.Enumerable.Contains:

using System.Linq;

var arr = new[] { "left", "right" };

if(arr.Contains(inputString)) { ... }