Home Dashboard Directory Help

The string.Contains method should include a signature accepting a Systen.StringComparison value by HitechRider



Sign in
to vote
Type: Suggestion
ID: 435324
Opened: 4/26/2009 8:46:26 AM
Access Restriction: Public


The framework provides both a .StartsWith and a .EndsWith method on strings that accept a System.StringComparison value but the equivalent string.Contains method is missing this signature.
Sign in to post a comment.
Posted by Sreejith K on 3/18/2014 at 1:50 PM
Indeed a required feature. Please prioritize
Posted by Daniel Smith on 2/5/2014 at 3:18 AM
We're 4 years, 9 months (!!!!) down the road now. Has this been enough time to organise getting this done? It's #4 in the list of top voted Visual Studio suggestions on Connect, and it looks really bad having it sitting there all this time.
Posted by mcbros on 3/23/2012 at 8:15 AM
I just checked the documentation for .NET 4.5 and still no Contains with StringComparison.CurrentCultureIgnoreCase, any new updates on this topic?
Posted by abatishchev on 2/13/2012 at 5:07 AM
Please add this into next .NET
Posted by Delzur on 2/10/2012 at 6:31 AM
Had this problem today.

An overriding method directly in the framework would be great/

I hope it's not abandoned.
Posted by Shimmy Weitzhandler on 10/26/2011 at 11:56 AM
Very compelling!
Posted by Daniel Smith on 8/27/2010 at 1:41 AM
True, we can workaround it using IndexOf, but it's always better and clearer to have an explicit method that does the job.

Plus by having a higher level abstraction with explicit intent, it creates opportunities for future speed optimizations, and also narrows down and focuses the BCL test cases.
Posted by MarcF on 8/26/2010 at 8:15 AM
Wasted time trying to find why I couldn't find this in the framework :(
Posted by Belinda H.1 on 10/30/2009 at 12:09 PM
It's a bit inconsistent that this isn't available in the framework. Often, little time savers like this make a big difference.
Posted by Microsoft on 5/19/2009 at 2:25 PM
Hi App,

Thanks for taking the time to file this suggestion. You're right that having the overload that takes a StringComparison would make the String.Contains API more complete. This isn't something we're planning to do for .NET Framework 4, but I'm going to leave this issue open so that we can continue to get feedback in the form of votes and comments. That will help us as we prioritize suggestions and potential features for the next release.

In the meantime, you can work around this by using the IndexOf function. I've modified your example to show how this would work here:

if (s.IndexOf("Microsoft RDP Audio Driver", StringComparison.CurrentCultureIgnoreCase) >= 0)

Base Class Libraries
Posted by Microsoft on 4/28/2009 at 5:30 AM
Thanks for reporting this issue. We are escalating this suggestion to the product unit who works on that specific feature area. The team will review this issue and make a decision on whether they will fix it or not for the next release.

Thank you,
Visual Studio Product Team
Sign in to post a workaround.
Posted by J Zelos on 6/20/2012 at 5:59 AM
VB Version (cannot recall where I got this from, its old)

Imports System.Runtime.CompilerServices

''' <summary>
''' This module holds extension methods for the string base class
''' </summary>
''' <remarks></remarks>
Module StringExtensions

    ''' <summary>
    ''' This overrides the contains method with a varient that accepts a String Comparision such as OrdinalIgnoreCase.
    ''' </summary>
    ''' <param name="source">The string to examine</param>
    ''' <param name="toCheck">The value to look for</param>
    ''' <param name="comp">A comparer such as OrdinalIgnoreCase</param>
    ''' <returns>true or false</returns>
    ''' <remarks></remarks>
    <Extension()> _
    Public Function Contains(ByVal source As String, ByVal toCheck As String, ByVal comp As StringComparison) As Boolean
        Return source.IndexOf(toCheck, comp) >= 0
    End Function

End Module
Posted by MarcF on 8/26/2010 at 8:16 AM
Use an extension method, eg:

public static bool Contains(this string str, string value, StringComparison comparisonType) {
return str.IndexOf(value, comparisonType) >= 0;