Is there any c# syntax that would make this if statement cleaner/shorter?
if (token == "(" || token == ")" || token == "+" || token == "-" || token == "*" || token == "/")
{
//do something
}
Like this:
// create a string with the valid chars
var tokens = "()+-*/";
// this will call the Contains method of the String class
if(tokens.Contains(token))
{
//do something
}
Or with an array: (This way you can validate on multiple chars within a match. (not included on this example))
// create an array with the valid strings
var tokens = new [] { "(", ")", "+", "-", "*", "/" };
// this will call Contains method of the Enumerable class
if(tokens.Contains(token))
{
//do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With