JimDel
JimDel

Reputation: 4359

Can I make a "Contains" Method be case insensitive with C#?

I'm using .NET 4.5 and C#. My code below works fine if the spelling is case sensitive. In other words if the file is spelled exactly like "SetupV8.exe". But I really need it to be case insensitive. I've played with it but cant find a way.

foreach (string file in directory.EnumerateFiles((AppDomain.CurrentDomain.BaseDirectory),  
         "*.exe", SearchOption.AllDirectories))
{
   if (!file.Contains("SetupV8.exe")
   {    
      // Do something
   }
}

Upvotes: 1

Views: 4656

Answers (6)

Chris Sinclair
Chris Sinclair

Reputation: 23198

file.ToLower().Contains("setupv8.exe") usually works fine. (though you might want to consider EndsWith instead)

Also, since EnumerateFiles returns FileInfo, you might as well check its Name property instead:

foreach (FileInfo file in directory.EnumerateFiles((AppDomain.CurrentDomain.BaseDirectory),  
         "*.exe", SearchOption.AllDirectories))
{
   if (!file.Name.ToLower().Contains("setupv8.exe")
   {    
      // Do something with file
   }
}

Also, if the name is "SetupV8.exe" and you don't expect it to be prefixed/suffixed with anything, perhaps just straight up check for equality at this point.

EDIT: Perhaps more importantly, you probably want to use just the file name. Unless you want to check if any part of the directory path matches. That is, you might not want c:\temp\setupv8.exe_directory\subdirectory\setupv8.exe to match as a false positive.

EDIT 8 years later for new readers: There are some edge cases where using ToLower() can introduce some unexpected results, so perhaps it might be preferable to use ToLowerInvariant() instead.

Upvotes: 0

Christopher Hackett
Christopher Hackett

Reputation: 6192

As per the MSDN article you can pass in StringComparison.OrdinalIgnoreCase to compare regardless of case.

file.name.Contains("SetupV8.exe", StringComparison.OrdinalIgnoreCase)

This will be more efficient as you don't create two mutalatable strings in the process and in my opinion looks cleaner than using .toLower()

However you should consider what you are checking here, would a file hash be better? You might be introducing a security problem if you are assuming the contents of the file is know.

Upvotes: 3

Olivier Jacot-Descombes
Olivier Jacot-Descombes

Reputation: 112259

If you want to compare the whole file name including the extension but without the directory:

file.Name.Equals(fileNameAndExt, StringComparison.OrdinalIgnoreCase)

file.FullName also includes the directory name. StringComparison.OrdinalIgnoreCase is the fastest comparison method as it does not apply culture specific treatments. This is the correct way to do it, since the file system doesn’t do it either.

Upvotes: 2

S3ddi9
S3ddi9

Reputation: 2151

just make an extension method

public bool Contains(this string my,string his)
 {
      return my.ToLower().Contains(his.ToLower());
 }

usage

....
if(file.Contains("SetupV8")) // the case is ignored !
....
....

Upvotes: 0

Steve
Steve

Reputation: 216243

string.Contains is just a wrapper around string.IndexOf as you can see from the NET sources

public bool Contains(string value)
{
    return (this.IndexOf(value, StringComparison.Ordinal) >= 0);
}

and string.IndexOf has a proper parameter to ignore the case of the string to search

 if (file.IndexOf("SetupV8.exe", StringComparison.OrdinalIgnoreCase) >= 0)
     // File found

StringComparison enum

Upvotes: 10

Fred Gassmann
Fred Gassmann

Reputation: 129

Just force your string to all lower case for the comparison..

file.ToLower().Contains("setupv8.exe")

Upvotes: 0

Related Questions