使用带有字符串键的哈希表/字典 不区分大小写的搜索

发布于 2024-07-19 02:19:32 字数 613 浏览 10 评论 0原文

想知道这是否可能。

我们有一个第 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

很糊涂小朋友 2024-07-26 02:19:33

使哈希表比较不区分大小写的代码

对于 2.0、3.0、3.5

Hashtable ht = new Hashtable(StringComparer.InvariantCultureIgnoreCase);

您可以在 this SO 链接

OR

Hashtable ht = System.Collections.Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable();

由于不区分大小写的字典集合非常常见,因此 .NET Framework 有一个 CollectionUtil 类,支持创建不区分大小写的 Hashtable 和 SortedList 对象。 通过调用 CreateCaseInsensitiveHashtable 或 CreateCaseInsensitiveSortedList 来使用。

对于.Net 1.0(我不确定1.0是否支持StringComparer)

public class InsensitiveComparer : IEqualityComparer
{
    CaseInsensitiveComparer _comparer = new CaseInsensitiveComparer();
    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLowerInvariant().GetHashCode();
    }

    public new bool Equals(object x, object y)
    {
        if (_comparer.Compare(x, y) == 0)
        {
            return true;
        }

        else
       {
           return false;
       }
    }
}

Hashtable dehash = new Hashtable(new InsensitiveComparer());

Code to make the hashtable comparisons case-insensitive

For 2.0, 3.0, 3.5

Hashtable ht = new Hashtable(StringComparer.InvariantCultureIgnoreCase);

You can get info on InvariantCultureIgnoreCase vs. OrdinalIgnoreCase on this SO link

OR

Hashtable ht = System.Collections.Specialized.CollectionsUtil.CreateCaseInsensitiveHashtable();

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)

public class InsensitiveComparer : IEqualityComparer
{
    CaseInsensitiveComparer _comparer = new CaseInsensitiveComparer();
    public int GetHashCode(object obj)
    {
        return obj.ToString().ToLowerInvariant().GetHashCode();
    }

    public new bool Equals(object x, object y)
    {
        if (_comparer.Compare(x, y) == 0)
        {
            return true;
        }

        else
       {
           return false;
       }
    }
}

Hashtable dehash = new Hashtable(new InsensitiveComparer());
暮凉 2024-07-26 02:19:33

使用字典:

new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

但更简单,我相信 StringDictionary 也不区分大小写:

    StringDictionary ht = new StringDictionary();
    ht.Add("MyKey", "Details");

    string result1 = ht["MyKey"];
    string result2 = ht["MYKEY"];
    string result3 = ht["mykey"];

With a dictionary:

new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);

but simpler, I believe StringDictionary is case-insensitive too:

    StringDictionary ht = new StringDictionary();
    ht.Add("MyKey", "Details");

    string result1 = ht["MyKey"];
    string result2 = ht["MYKEY"];
    string result3 = ht["mykey"];
猫弦 2024-07-26 02:19:33

您可以向 HashTable 构造函数提供不区分大小写的 IEqualityComparer,但这要求您可以影响第 3 方库中的 HashTable 构造。

如果您从代码中填充哈希表,则可以在插入时规范化键并再次检索规范化键。

如果您无法影响哈希表的内容,但您知道键的结构,则可以在访问哈希表之前更正用户输入。

You can supply an case-insensitive IEqualityComparer to the HashTable constructor, but this requires that you can influence the HashTable 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.

心病无药医 2024-07-26 02:19:33

这不是一种非常有效的方法,但您始终可以获取哈希表并从中创建 StringDictionary:

Hashtable ht = new Hashtable();
ht.Add("FOO", "bar");

StringDictionary dict = new StringDictionary();

foreach(string key in ht.Keys)
    dict.Add(key, ht[key].ToString());

string result = dict["foo"]; // Assigns "bar" to result

This isn't a very efficient way, but you could always take the hashtable and make a StringDictionary from it:

Hashtable ht = new Hashtable();
ht.Add("FOO", "bar");

StringDictionary dict = new StringDictionary();

foreach(string key in ht.Keys)
    dict.Add(key, ht[key].ToString());

string result = dict["foo"]; // Assigns "bar" to result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文