[英]More Efficient OR Statement
I currently have C# code checking for user input as such: 我目前有C#代码检查用户输入,例如:
if (e.KeyCode == Keys.Enter && InputTextbox.Text.Contains("good morning")
||
e.KeyCode == Keys.Enter && InputTextbox.Text.Contains("morning"))
Is there another way to use the || 还有使用||的另一种方法 "or" statement so that it can all be in the one line?
“或”语句,以便它们全部都可以在一行中? Something like:
就像是:
if (e.KeyCode == Keys.Enter && InputTextbox.Text.Contains("good morning" || "morning")
var allowedText = new List<string> { "good morning", "morning" };
if (e.KeyCode == Keys.Enter && allowedText.Contains(InputTextbox.Text))
{
// do something
}
You can create an array with the values you want to check and then check if the array contains the TextBox value 您可以使用要检查的值创建一个数组,然后检查该数组是否包含TextBox值
string[] a = {"good morning" , "morning");
if (e.KeyCode == Keys.Enter && a.Contains(InputTextbox.Text))
{
}
Hm, for instance: if your InputTextBox is: "Hello good morning" I don't think the above answers will work, if this is what you seek (having a posibly larger string checked against the given smaller strings), you have to check it the other way around: 嗯,例如:如果您的InputTextBox是:“您好,早上好”,我认为上述答案不起作用,如果您要这样做(必须将给定的较小字符串与可能较大的字符串进行比较),则必须检查反过来:
if (e.KeyCode == Keys.Enter && (InputTextbox.Text.Contains("good morning") || InputTextbox.Text.Contains("morning"))
Just thought I'd point it out, if not the case ignore this answer. 只是以为我会指出,如果不是这种情况,请忽略此答案。
This is sufficient: 这足够了:
if (e.KeyCode == Keys.Enter && InputTextbox.Text.Contains("morning"))
{
}
It is because if InputTextbox.Text.Contains("good morning")
is true
, InputTextbox.Text.Contains("morning")
must be true
as well. 这是因为,如果
InputTextbox.Text.Contains("good morning")
为true
,则InputTextbox.Text.Contains("morning")
必须为true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.