.NET如何比较两个表示文件名的字符串,以正确忽略大小写 [英] .NET How to compare two Strings that represent filenames ignoring case correctly
问题描述
String fileA
和 String fileB <
$ b fileA.Equals(fileB,StringComparison.CurrentCultureIgnoreCase)
然后问题就变成了我应该使用哪种文化,缺省的当前(ui?)文化是否足够?我似乎无法找到任何BCL方法。
OrdinalIgnoreCase
,详见在字符串中使用字符串的最佳实践.NET Framework (搜索文件路径以查找相关部分)。如果使用区域性匹配字符串,可能会得到在一个例子中,名称häl.gif和hal.gif将被视为匹配。
Given that (at least on NTFS) the filesystem on Windows is case insensitive, I would like to compare String fileA
to String fileB
as such:
fileA.Equals(fileB, StringComparison.CurrentCultureIgnoreCase)
The question then becomes which culture I should use, does the default current (ui?) culture suffice? I can't seem to find any BCL methods for this purpose.
You should use StringComparison.OrdinalIgnoreCase
, as is detailed in Best Practices for Using Strings in the .NET Framework (search for "file paths" to find the relevant section).
If you use a culture for matching the strings, you may get in a sitation where for example the names "häl.gif" and "hal.gif" would be considered a match.
这篇关于.NET如何比较两个表示文件名的字符串,以正确忽略大小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!