alexy12
alexy12

Reputation: 535

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?

Upvotes: 0

Views: 147

Answers (3)

Andriy Kizym
Andriy Kizym

Reputation: 1796

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()

Upvotes: 2

HatSoft
HatSoft

Reputation: 11201

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())) 

Upvotes: 1

Narendra
Narendra

Reputation: 3117

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

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

Upvotes: 1

Related Questions