简体   繁体   English

我可以使用C#使“包含”方法不区分大小写吗?

[英]Can I make a “Contains” Method be case insensitive with C#?

I'm using .NET 4.5 and C#. 我正在使用.NET 4.5和C#。 My code below works fine if the spelling is case sensitive. 如果拼写区分大小写,则我的以下代码可以正常工作。 In other words if the file is spelled exactly like "SetupV8.exe". 换句话说,如果文件的拼写与“ 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 从NET资源中可以看到, string.Contains只是string.IndexOf的包装。

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 和string.IndexOf具有适当的参数来忽略要搜索的字符串的大小写

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

StringComparison enum StringComparison枚举

As per the MSDN article you can pass in StringComparison.OrdinalIgnoreCase to compare regardless of case. 根据MSDN文章,您可以传入StringComparison.OrdinalIgnoreCase进行比较(无论大小写)。

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() 这将更加有效,因为您不会在此过程中创建两个可变字符串,而且我认为比使用.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. file.FullName还包括目录名称。 StringComparison.OrdinalIgnoreCase is the fastest comparison method as it does not apply culture specific treatments. StringComparison.OrdinalIgnoreCase是最快的比较方法,因为它不应用特定于文化的处理方法。 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. file.ToLower().Contains("setupv8.exe")通常可以正常工作。 (though you might want to consider EndsWith instead) (尽管您可能想考虑使用EndsWith)

Also, since EnumerateFiles returns FileInfo , you might as well check its Name property instead: 另外,由于EnumerateFiles返回FileInfo ,因此您也可以检查其Name属性:

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. 另外,如果名称 “ SetupV8.exe”,并且您不希望它带有任何前缀/后缀,那么此时也许直接检查是否相等即可。

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. 也就是说,您可能不希望将c:\\temp\\setupv8.exe_directory\\subdirectory\\setupv8.exe匹配为误报。

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. 8年后针对新读者的编辑:在某些情况下,使用ToLower() 可能会带来一些意外的结果 ,因此也许最好使用ToLowerInvariant()

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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