Search code examples
c#string-comparisonignore-case

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


Solution

  • You can use regular expression to match a string search in C#. You also have the option to ignore case.

    if (System.Text.RegularExpressions.Regex.IsMatch(s, sPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase))
    

    This link might be useful: How to: Search Strings Using Regular Expressions (C# Programming Guide)