简体   繁体   中英

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
   }
}

Thanks

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

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.

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.

只需将您的字符串全部小写以进行比较。

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

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.

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 !
....
....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM