开发者

How to use StringComparison for strings in C#?

开发者 https://www.devze.com 2023-03-23 08:23 出处:网络
string body = Selenium.GetBodyText(); if (body.Contains(\"software\", StringComparison.CurrentCultureIgnoreCase))
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 th开发者_StackOverflow社区e 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).


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)


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.


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)


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

bodyText.Contains("software");


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


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


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


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;
    }
}


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

eg:

"samplestring".ToUpper().Contains("SAMPLESTRING")


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消