使用带有字符串键的哈希表/字典 不区分大小写的搜索
想知道这是否可能。
我们有一个第 3 方库,其中包含有关用户的识别信息...
与该库的主要交互是通过一个哈希表,该哈希表以字符串为键,并返回该键信息的对象图。
问题是,密钥显然是区分大小写的,但是我们从用户浏览器获得的内容不一定与大小写匹配......(我们经常得到完全小写的密钥)
我想知道是否可以做一个针对哈希表的不敏感键搜索。
例如,
Hashtable ht = new Hashtable();
ht.Add("MyKey", "Details");
string result = ht["MyKey"];
string result = ht["MYKEY"];
string result = ht["mykey"];
如果我们可以向公司提交支持票以添加此功能,是否有任何其他 DataStructures(即新的通用集合/字典)支持此功能
最后,是否可以覆盖 System.String GetHashCode () 方法,至 使所有大小写不变的字符串返回相同的哈希码...例如,我认为这是一个不行的事情,因为 string
是一个密封类,
如果有人有任何建议,干杯
Wondering if this is possible.
We have an 3rd Party library that contains identification information about users...
The main interaction with the library is through a HashTable which is keyed with a string, and returns an Object Graph of information for that key.
The problem is, the key is obviously Case Sensitive, but what we get from the users browser doesn't necessarily match the case... (We often get the key fully lowercase'd)
I'm wondering if it's possible to do a case Insensitive key search against a hashtable.
e.g.
Hashtable ht = new Hashtable();
ht.Add("MyKey", "Details");
string result = ht["MyKey"];
string result = ht["MYKEY"];
string result = ht["mykey"];
On the off chance we could submit a support ticket to the company to add this functionality, are there any other DataStructures (i.e. the new generic collections/dictionaries) that support this functionality
Lastly, would it be possible to override the System.String GetHashCode() method, to
make all case invariant strings return the same hashcode... e.g. I'm thinking this is a no goer as string
is a sealed class
Cheers if anyone has any suggestions
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使哈希表比较不区分大小写的代码
对于 2.0、3.0、3.5
您可以在 this SO 链接
OR
由于不区分大小写的字典集合非常常见,因此 .NET Framework 有一个 CollectionUtil 类,支持创建不区分大小写的 Hashtable 和 SortedList 对象。 通过调用 CreateCaseInsensitiveHashtable 或 CreateCaseInsensitiveSortedList 来使用。
对于.Net 1.0(我不确定1.0是否支持StringComparer)
Code to make the hashtable comparisons case-insensitive
For 2.0, 3.0, 3.5
You can get info on InvariantCultureIgnoreCase vs. OrdinalIgnoreCase on this SO link
OR
Because case-insensitive dictionary collection is such a common use, the .NET Framework has a CollectionUtil class that supports creating Hashtable and SortedList objects that are case insensitive. Use by calling CreateCaseInsensitiveHashtable or CreateCaseInsensitiveSortedList.
For .Net 1.0 (I am not sure if 1.0 supports StringComparer)
使用字典:
但更简单,我相信
StringDictionary
也不区分大小写:With a dictionary:
but simpler, I believe
StringDictionary
is case-insensitive too:您可以向
HashTable
构造函数提供不区分大小写的IEqualityComparer
,但这要求您可以影响第 3 方库中的HashTable
构造。如果您从代码中填充哈希表,则可以在插入时规范化键并再次检索规范化键。
如果您无法影响哈希表的内容,但您知道键的结构,则可以在访问哈希表之前更正用户输入。
You can supply an case-insensitive
IEqualityComparer
to theHashTable
constructor, but this requires that you can influence theHashTable
construction in the 3rd party library.If you fill the hash table from your code, you could normalize the keys on insertion and retrieve normalized keys again.
If you are unable to influence the content of the hash table, but you know the structure of the keys, you could correct the user input before accessing the hash table.
这不是一种非常有效的方法,但您始终可以获取哈希表并从中创建 StringDictionary:
This isn't a very efficient way, but you could always take the hashtable and make a StringDictionary from it: