Поделиться через


String.Contains really should have an overload with StringComparison specified

I've had this come up in conversations in the past (and it's been on feedback for almost 3 years now), but just realized I never posted the (simple) extension method I use.

A comment on Brad's blog:

  • # Milan Negovan said on July 4, 2005 5:59 PM:
    Brad, is there any reason why Contains, IndexOf and such don't have an override with StringComparison?

(Whidbey actually added it for IndexOf and LastIndexOf, as per the feedback link)

A comment on the BCL team blog:

Thursday, April 06, 2006 3:13 AM by Andrew Webb

# Finish off the String class

Add a StringComparison parameter to the Contains and Replace methods of the String class.

While I could add it for Replace as well, I only typically need Contains.

     public static class ExtensionMethods
    {
        // BCL should really include this
        public static bool Contains(this string self, string value, StringComparison comparison)
        {
            return self.IndexOf(value, comparison) >= 0;
        }
    }

Comments