Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use StringComparison for strings in C#?

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

like image 596
Maya Avatar asked Jul 28 '11 13:07

Maya


2 Answers

I don't believe string has an overload of Contains taking a StringComparison. However, you could use IndexOf which does:

if (body.IndexOf("software", StringComparison.CurrentCultureIgnoreCase) != -1)
like image 64
Jon Skeet Avatar answered Oct 22 '22 18:10

Jon Skeet


String.Contains only take one parameter - your code should be

bodyText.Contains("software");
like image 24
Morten Anderson Avatar answered Oct 22 '22 17:10

Morten Anderson