.NET如何比较两个表示文件名的字符串,以正确忽略大小写 [英] .NET How to compare two Strings that represent filenames ignoring case correctly

查看:162
本文介绍了.NET如何比较两个表示文件名的字符串,以正确忽略大小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设(至少在NTFS上)Windows上的文件系统不区分大小写,我想比较 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆