简体   繁体   English

不等字符串

[英]Not equal string

I'm trying to set a condition like this我正在尝试设置这样的条件

if (myString=!"-1")
{
  //Do things
}

But it fails.但它失败了。 I've tried我试过了

if(myString.Distinct("-1")) 
{
  //Do things
}

but it doesn't work either.但它也不起作用。

Try this:试试这个:

if(myString != "-1")

The opperand is != and not =!操作数是!=而不是=!

You can also use Equals您也可以使用Equals

if(!myString.Equals("-1"))

Note the !注意! before myString在 myString 之前

It should be this:应该是这样的:

if (myString != "-1")
{
    //Do things
}

Your equals and exclamation are the wrong way round.你的等号和感叹号是错误的。

使用Equals()您还可以使用Yoda 条件

if ( ! "-1".Equals(myString) )

Now in C# 9, it can be done in a more readable way like this.现在在 C# 9 中,它可以像这样以更具可读性的方式完成。

string  str = "x";
if(str is not "y")
{
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM