6

The Contains(...) method for string is case-sensitive. I'd like to override it in order to make it case-INsensitive with the following code (which is stolen from here):

public static bool Contains(this string source, string toCheck, StringComparison comp)
{
    return source.IndexOf(toCheck, comp) >= 0;
}

However, I don't know where the code should be pasted. Should it be placed inside the same namespace of the class program? Does it need a dedicated class?

Community
  • 1
  • 1
user1154138
  • 193
  • 1
  • 3
  • 9
  • 1
    You can't inherit from the string class since it's sealed. – Jeb Jan 18 '12 at 13:08
  • 3
    @JohnParr Yes, but the question's title and text is missleading. He's actually using an extension method - which the OP doesn't seem to really know nor understand (yet). – Christian.K Jan 18 '12 at 13:10

8 Answers8

10

If what you are intending is to create an extension method for the string class, then you need to put it inside some class. To use it, simply make sure you have a using statement specifying a reference to the namespace containing the class.

For example:

namespace SomeNamespace
{
    public static class StringExtensions
    {
        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }
    }
}

// ... In some other class ...
using SomeNamespace;

// ...
bool contains = "hello".Contains("ll", StringComparison.OrdinalIgnoreCase);
Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
2

As you are creating an extension method, you can put it into any namespace and any static class:

namespace MyProject.MyNamespace
{
    public static class MyExtensions
    {
        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }
    }
}

This is purely an organisational thing. You will need to add the necessary using MyProject.MyNamespace; statements where ever you use the code of course.

ChrisF
  • 134,786
  • 31
  • 255
  • 325
1

You'll need to put this extension method somewhere where it makes sense for you, for instance a utility class, which has to be static. You can't actually override anything from string, that's why you're using the extension method.

Example:

public static class StringExtension
{
    public static bool Contains(this string source, string toCheck, StringComparison comp)
    {
        return source.IndexOf(toCheck, comp) >= 0;
    }
}
diggingforfire
  • 3,359
  • 1
  • 23
  • 33
1

This is an Extension Method. You could just create a class called MyStringExtension. But the name is not really relevant. As long as the code file has the namespace defined, where your MyStringExtension class is defined, all your strings will have that method.

dowhilefor
  • 10,971
  • 3
  • 28
  • 45
1

This method is called Extension method

You can have it in your custom namespace within static class and use it by using your custom namespace.

Azodious
  • 13,752
  • 1
  • 36
  • 71
1

Instead overriding (which you can't do) or creating an extension method, I suggest you use the following Contains overload and write custom IEqualityComparer instead.

public static bool Contains<TSource>(
    this IEnumerable<TSource> source, 
    TSource value, 
    IEqualityComparer<TSource> comparer);
Tomislav Markovski
  • 12,331
  • 7
  • 50
  • 72
  • You might want to change "overwriting extension method" (which is also not possible as they are static methods really) into "creating an extension method". People, including myself, are a bit nitpicky ;-) – Christian.K Jan 18 '12 at 13:13
1

That is an extension method, so it will be available on any string object that has access to your extension method.

You might define it like this

namespace Example.ExtensionMethods
{
    public static class StringExtensions
    {
        public static bool Contains(this string source, string toCheck, StringComparison comp)
        {
            return source.IndexOf(toCheck, comp) >= 0;
        }
    }   
}

And use like this

using Example.ExtensionMethods;

string s = "foobar";
int i = s.Contains("oob", StringComparison.InvariantCultureIgnoreCase);
Samuel Slade
  • 8,405
  • 6
  • 33
  • 55
Sam Greenhalgh
  • 5,952
  • 21
  • 37
0

I would suggest putting it in some sort of utility class that you make, which contains nice helper methods such as this. If you added it to, say, a StringUtils class you could just call StringUtils.Contains() to use it.

jprofitt
  • 10,874
  • 4
  • 36
  • 46
  • 2
    Not exactly, the method he posted is an extension method. So he wouldn't need the CtringUtils.Contains he could just directly call contains on his string. – dowhilefor Jan 18 '12 at 13:10
  • @dowhilefor I'm mostly a Java developer, so you'll have to excuse me ;) – jprofitt Jan 18 '12 at 13:14