简体   繁体   中英

does contain and does not contain in same if

For some reason i cannot get this if statement to work

if (htmlCode.Contains("Sign out") && !htmlCode.Contains("bye bye")) 
{
    // do stuff...
}

is there any way to get contains and does not contain to work in same if statement?

First of all check the htmlCode, the text could be mixed with some html tags or something like that, also the issue can be with cases, when trying to find some string in the text you should always remember about cases.

You can use .Contains method or .IndexOf , actually the contains method in the Framework is implemented like this:

public bool Contains(string value)
{
  return this.IndexOf(value, StringComparison.Ordinal) >= 0;
}

For comparing large strings without knowing the case I would use:

htmlCode.IndexOf("Sign out", StringComparison.InvariantCultureIgnoreCase);
htmlCode.IndexOf("Bye bye", StringComparison.InvariantCultureIgnoreCase);

If you know that response will be small, you can use .ToLower() or .ToUpper()

Try to compare by converting either upper case or lower case.

if (htmlCode.ToUpper().Contains("SIGN OUT") && !htmlCode.ToUpper().Contains("BYE BYE")) 
{
    // do stuff...
}

You if clause works correctly

It might be not working because of the string case

So i would suggest you do it this way

if (htmlCode.ToUpper().Contains("Sign out".ToUpper()) && !htmlCode.ToUpper().Contains("bye bye".ToUpper())) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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