75

How can I make the following case insensitive?

myString1.Contains("AbC")
CJ7
  • 20,640
  • 59
  • 173
  • 305
  • 1
    Or duplicate of MSDN article for [String.Contains](http://msdn.microsoft.com/en-us/library/dy85x1sa.aspx) which suggests to "See also: IndexOf"... – Alexei Levenkov Jul 10 '13 at 06:50
  • Well, this question is not a 100%-duplicate if it asks for answers in VB.Net, since VB.Net has features C# does not have, like the `LIKE` operator which could be used here. – sloth Jul 10 '13 at 07:49
  • He tagged both languages, so probably he doesn't care about which one the solution is, so the duplicate completly answer this question. Anyway, if he edits his question with onlt vb.net then it will enter the reopen queue automatically. – SysDragon Jul 10 '13 at 08:01

3 Answers3

142

You can create your own extension method to do this:

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

And then call:

 mystring.Contains(myStringToCheck, StringComparison.OrdinalIgnoreCase);
Tobia Zambon
  • 7,140
  • 2
  • 31
  • 68
  • 2
    This is arguably the best answer by wrapping it in an extension method. – Moo-Juice Jul 10 '13 at 06:45
  • 7
    @Moo-Juice Yes because it is familiar :) http://stackoverflow.com/a/444818/447156 – Soner Gönül Jul 10 '13 at 06:48
  • 5
    I would recommend calling the function something other than an exact match with something in the framework like your initials + Contains. I use this approach for a ton of things. My initials are PS so I have psContains, psStartsWith, psEndsWith, etc. The reason I think it should be different is so you can tell, just by reading the code, what it's going to do. Also, the if the signatures are compatible, the compiler might not like it. If they are compatible enough, compiling wouldn't help find you find out if your extensions are in play. Extensions are a real gem of Dotnet. If used correctly :) – Programmer Paul Jan 21 '14 at 15:23
  • I wish I could give you more than one up-vote! – pjdupreez Nov 05 '14 at 07:58
  • You should check to see if the string and the toCheck argument are null. If either is null then you should return false. Otherwise it is going to throw a exception. – ashlar64 Feb 23 '15 at 21:28
51

You can use:

if (myString1.IndexOf("AbC", StringComparison.OrdinalIgnoreCase) >=0) {
    //...
}

This works with any .NET version.

joe
  • 7,264
  • 7
  • 45
  • 73
  • 3
    Two typos in your answer, otherwise good ;-) It should be (obviously) `StringComparison.OrdinalIgnoreCase` – andreas Jan 13 '15 at 13:52
11
bool b = list.Contains("Hello", StringComparer.CurrentCultureIgnoreCase);

[EDIT] extension code:

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

This could work :)

Kamil Budziewski
  • 21,193
  • 12
  • 77
  • 95