将 C# 中的字符串与 if 语句中的 OR 进行比较

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11283764/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 17:15:06  来源:igfitidea点击:

Comparing strings in C# with OR in an if statement

c#string

提问by Ryan

I'm a newbie to C#, but can't seem to find anything about this problem. Here's what I'm trying to do:

我是 C# 的新手,但似乎无法找到有关此问题的任何信息。这是我想要做的:

string testString = txtBox1.Text;
string testString2 = txtBox2.Text;

if ((testString == "") || (testString2 == ""))
{
    MessageBox.Show("You must enter a value into both boxes");
    return;
} 

Basically I need to check to see if either txtBox1 or txtBox2 is blank. However I'm getting an error when running this. What's the proper way to do this (or is my approach all wrong)?

基本上我需要检查 txtBox1 或 txtBox2 是否为空白。但是我在运行这个时遇到错误。这样做的正确方法是什么(或者我的方法全错了)?

采纳答案by Habib

Since you want to check whether textboxes contains any value or not your code should do the job. You should be more specific about the error you are having. You can also do:

由于您想检查文本框是否包含任何值,因此您的代码应该完成这项工作。您应该更具体地了解您遇到的错误。你也可以这样做:

if(textBox1.Text == string.Empty || textBox2.Text == string.Empty)
   {
    MessageBox.Show("You must enter a value into both boxes");
   }

EDIT 2: based on @JonSkeet comments:

编辑 2:基于@JonSkeet 评论:

Usage of string.Compare is not required as per OP's original unedited post. String.Equalsshould do the job if one wants to compare strings, and StringComparisonmay be used to ignore case for the comparison. string.Compare should be used for order comparison. Originally the question contain this comparison,

根据 OP 的原始未编辑帖子,不需要使用 string.Compare。如果想要比较字符串,String.Equals应该可以完成这项工作,并且StringComparison可以用来忽略比较的大小写。string.Compare 应该用于订单比较。最初这个问题包含这个比较,

string testString = "This is a test";
string testString2 = "This is not a test";

if (testString == testString2)
{
    //do some stuff;
}

the if statement can be replaced with

if 语句可以替换为

if(testString.Equals(testString2))

or following to ignore case.

或以下忽略大小写。

if(testString.Equals(testString2,StringComparison.InvariantCultureIgnoreCase)) 

回答by wquist

use if (testString.Equals(testString2)).

使用if (testString.Equals(testString2)).

回答by oxygenpt

try

尝试

if (testString.Equals(testString2)){
}

回答by Tibi

The code provided is correct, I don't see any reason why it wouldn't work. You could also try if (string1.Equals(string2))as suggested.

提供的代码是正确的,我看不出它为什么不起作用的任何原因。您也可以if (string1.Equals(string2))按照建议尝试。

To do if (something OR something else), use ||:

要做到这一点if (something OR something else),请使用||

if (condition_1 || condition_2) { ... }

回答by Code0987

Here's a more valid way which also check if your textbox is filled with only blanks.

这是一种更有效的方法,它也可以检查您的文本框是否只填充了空格。

// When spaces are not allowed
if (string.IsNullOrWhiteSpace(txtBox1.Text) || string.IsNullOrWhiteSpace(txtBox2.Text))
  //...give error...

// When spaces are allowed
if (string.IsNullOrEmpty(txtBox1.Text) || string.IsNullOrEmpty(txtBox2.Text))
? //...give error...

The edited answer of @Habib.OSU is also fine, this is just another approach.

@Habib.OSU 的编辑答案也很好,这只是另一种方法。

回答by Marrca35

Try:

尝试:

    if (textBox1.Text == "" || textBox2.Text == "")
    {
        // do something..
    }

Instead of:

代替:

    if (textBox1.Text == string.Empty || textBox2.Text == string.Empty)
    {
        // do something..
    }

Because string.Empty is different than - "".

因为 string.Empty 不同于 - ""。