Jul 08

Ok, so you already know the answer, right? That’s why the String class has the Contains method. And it will work until you need to explicitly need to use a different StringComparison option than the one that is used by default.

If like me, you know that Reflector is your friend, then you can just open it and see how the Contains method is implemented. Basically, what you need is a new extension method that looks like this:

public static class StringExtensions    {
        public static Boolean Contains(this String str, String value, StringComparison options)   {
            return str.IndexOf(value, options) >= 0;

Which means that you’ll be able to use it like this:

var name = “luis”;
var anotherName = “LUIS MIGUEL”;
var exists = anotherName.Contains(“luis”, StringComparison.CurrentCultureIgnoreCase );

Bottom line: you simply cannot live without Reflector and C# 3.0!

2 comments so far

  1. Waleed Eissa
    2:32 pm - 7-15-2008

    > you simply cannot live without Reflector and C# 3.0!

    I agree with the Reflector part but why C# 3.0? I use C# 2.0 and I”m very comfortable with it (planning to upgrade soon though) .. but initially I have read about the new features and I don”t find any of them really tempting esp. that it still uses CLR 2.0 (but probably I can”t judge correctly as I didn”t use it), so, what do you like about C# 3.0? (may be you”ll encourage me to upgrade even sooner :))

  2. luisabreu
    6:25 pm - 7-15-2008


    I love everything about C# 3.0 🙂 You”ll see that after using them, you cannot live without them. Linq To Objects is simply great, but let”s forget about it for a minute…let”s think about lambda expressions! with it I”m able to write a method in place and let the compiler infer types of parameters and return types. Just by using this expressions, you”ll end up with some pretty cool stuff.

    Do yourself a favor and start using C# 3.0 and then you”ll see what I mean 🙂