简体   繁体   English

是否包含和不包含相同

[英]does contain and does not contain in same if

For some reason i cannot get this if statement to work 由于某种原因,我无法获得此if语句

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? 有什么办法可以包含内容,并且不包含要在相同的if语句中工作的方式?

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. 首先检查htmlCode,文本可能会与一些html标记或类似内容混合在一起,问题也可能与大小写有关,当尝试在文本中查找某些字符串时,您应该始终记住大小写。

You can use .Contains method or .IndexOf , actually the contains method in the Framework is implemented like this: 您可以使用.Contains方法或.IndexOf ,实际上Framework中的contains方法是这样实现的:

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() 如果您知道响应会很小,则可以使用.ToLower().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 您if子句正常工作

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

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

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