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

Maya
  • 7,053
  • 11
  • 42
  • 53

10 Answers10

12

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)
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    wouldn't the error message indicate that he is using an older framework? It seems the error message would be different if the method signature was wrong. –  Jul 28 '11 at 13:58
  • 1
    @0A0D: Good point - it should be "No overload for method Contains takes 2 arguments". I guess we'll see what Maya says :) – Jon Skeet Jul 28 '11 at 14:06
  • @Maya Another possibility would be to extend the `String` class with an extension method using code like above. You would essentially be creating your own overload. – George Johnston Jul 28 '11 at 14:07
  • @Maya: I'm sorry Maya, I hope you are not offended. It was meant as generality. –  Jul 28 '11 at 14:08
  • body is a string, and when I do body.Contains, I see 3 overloads - one taking char, another taking string, and the last one taking char with StringComparison. You are right Jon. Thanks! I am not sure how to check the version of what framework I am using.I will try using IndexOf. – Maya Jul 28 '11 at 14:14
  • @ 0A0D: That's ok :) I'm not offended. Thanks for trying to help me :) – Maya Jul 28 '11 at 14:16
  • @Maya: I do not see the three overloads you are talking about. What IDE? In VS 2010, I see the standard Contains(string) method and another one using IEnumerable. –  Jul 28 '11 at 14:31
7

I'm not sure if you are using .NET 1.1, but it did not contain the method Contains. You have to use IndexOf. .NET 2.0 added the method Contains (per MSDN). With IndexOf, you can use StringComparison.

5

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)

rizalp1
  • 6,346
  • 2
  • 17
  • 19
3

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

bodyText.Contains("software");
Morten Anderson
  • 2,301
  • 15
  • 20
2

Contains has only one parameter - the string it is comparing against. Did you mean, Equals which does take a StringComparison?

ginbot
  • 303
  • 2
  • 6
1

Well you could always make an extension method (if you're using .Net 3.5 or higher):

public static class StringExtensions
{
    /// <summary>
    /// Returns a value indicating whether the specified String object 
    /// occurs within this string.
    /// </summary>
    /// <param name="str">string object being extended</param>
    /// <param name="value">string object to check for</param>
    /// <param name="comparer">StringComparer to use</param>
    public static bool Contains(this string str, string value, StringComparer comparer)
    {
        StringComparison comparison;
        if (comparer == StringComparer.CurrentCulture)
            comparison = StringComparison.CurrentCulture;
        else if (comparer == StringComparer.CurrentCultureIgnoreCase)
            comparison = StringComparison.CurrentCultureIgnoreCase;
        else if (comparer == StringComparer.InvariantCulture)
            comparison = StringComparison.InvariantCulture;
        else if (comparer == StringComparer.InvariantCultureIgnoreCase)
            comparison = StringComparison.InvariantCultureIgnoreCase;
        else if (comparer == StringComparer.Ordinal)
            comparison = StringComparison.Ordinal;
        else if (comparer == StringComparer.OrdinalIgnoreCase)
            comparison = StringComparison.OrdinalIgnoreCase;
        else
            comparison = StringComparison.Ordinal;

        if (str.IndexOf(value, comparison) != -1)
            return true;
        else
            return false;
    }
}
Christopher Stevenson
  • 2,843
  • 20
  • 25
1

From the code what has been pasted, you are declaring a variable "body" of type string and using another variable "bodyText" which is undeclared.

Deepansh Gupta
  • 593
  • 4
  • 9
1

String doesn't have a Contains method with that signature. str.Contains(chr, StringComparison), not str.Contains(string, StringComparison)...

Brian
  • 2,772
  • 15
  • 12
0

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.

David G
  • 161
  • 2
  • 6
0

You can still use Contains provided you compare it after converting it to same case(UPPER or lower)

eg:

"samplestring".ToUpper().Contains("SAMPLESTRING")
Arjun Shetty
  • 1,575
  • 1
  • 15
  • 36
  • 3
    Not a good idea, due to the way some cultures handle upper-casing. For example, `"mail".ToUpper().Contains("MAIL")` may not return true. – Jon Skeet Jul 28 '11 at 14:06
  • thanks skeet.didn think over. how about converting the argument too to uppercase and comparing. ("samplestring".ToUpper().Contains("samplestring".ToUpper())) – Arjun Shetty Jul 28 '11 at 14:14
  • 1
    I'm not even sure whether that will work. Basically performing case-insensitive matching ideally shouldn't involve conversion, as far as I'm aware. – Jon Skeet Jul 28 '11 at 15:00