How to use StringComparison for strings in C#?

Viewed 7201
string body = Selenium.GetBodyText();

if (body.Contains("software", StringComparison.CurrentCultureIgnoreCase))
{
   //do something
}

I get a string does not contain a definition for Contains message when I do the above. What am I doing wrong here? Thanks in advance everyone!

I am trying to check if body has the string "software", "Software" or "SOFTWARE". body will contain paragraphs and paragraphs of text (string).

10 Answers

I'm very late to this party, but hopefully I can save someone from the hole I was just in.

With .net core 2.1 overload Compare(String, StringComparison) was made available. As of this writing, if you're using .net Framework or an earlier version of core, you'll need to use a different solution out lined here.

Grip: Could MSFT please keep the framework selector in one place on the api docs page? Used to be top and center and the "Applies to" is very misleading.

Related