c# - 在 IF 子句中 OR'ing 多个值选项的替代方法是什么?

标签 c# if-statement

是否有任何 C# 语法可以使此 if 语句更清晰/更短?

if (token == "(" || token == ")" || token == "+" || token == "-" || token == "*" || token == "/")
{
     //do something
}

最佳答案

像这样:

// 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
}

或者使用数组:(这样您就可以在一个匹配项中验证多个字符。(不包括在此示例中))

// 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
}

关于c# - 在 IF 子句中 OR'ing 多个值选项的替代方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39568660/

相关文章:

c# - 正则表达式 "NOT"存在模式

c# - 使用 Entity Framework 保存 AutoMapper 映射的实体集合

java - 我必须为一个程序编写两个方法,当用户提示宽度和高度时计算矩形的面积和周长?

c - 为什么 C 中 if 语句中的 (a == b == c) 不起作用?

javascript - 为什么我的函数只在按钮的 onClick 事件上运行一次?

c# - FirstOrDefault 函数默认值

c# - 如何检查目录是否共享?

c# - 如何检查我的应用程序与 .net 核心的兼容性?

Java:如何防止使用条件语句将重复项添加到 ArrayList 中?

if-statement - Ocaml if-then-else 语法错误