如何不区分大小写获取集合类中的某一元素

黄土豆 2008-04-27 07:47:36
在.net C#的各种集合类中,如果键是一个字符串,有没有可以不区分字符串大小写就能索引集合里的元素的简单方法或某种特定的集合。
不包括遍历。
...全文
183 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
黄土豆 2008-04-28
  • 打赏
  • 举报
回复
谢谢 whycom 了,我那天也看到这个帮助,但只看了个开头就没有看下去,没想到下面就有这种办法的说明。

wuyazhe 的方法也很有启发,多谢。
黄土豆 2008-04-27
  • 打赏
  • 举报
回复
我想知道的是有没有简单点的办法,一定要继承吗?
ivorstar 2008-04-27
  • 打赏
  • 举报
回复
一个方法是将使用key的地方都只出现大写。
比如继承HashTable类,然后override它的Add()方法和索引器,当然还有其它许多方法需要重写,避免错误的key类型导致异常……
    class MyHash : System.Collections.Hashtable
{
public override void Add(object key, object value)
{
if (key is string)
{
string s = ((string)key).ToUpper();
base.Add(key, value);
}
else
{
throw new ArgumentException("key is not a string");
}
}

public override object this[object key]
{
get
{
if (key is string)
{
string s = ((string)key).ToUpper();
return base[s];
}
else
{
throw new ArgumentException("key is not a string");
}
}
set
{
base[key] = value;
}
}
}
wucy2008 2008-04-27
  • 打赏
  • 举报
回复


`` 不可以用泛型集合吗``?


` 直接使用``


` ` 避免大小写问题```


`
whycom 2008-04-27
  • 打赏
  • 举报
回复
使用 CollectionsUtil.CreateCaseInsensitiveHashTable 方法
CollectionsUtil.CreateCaseInsensitiveHashTable 方法是一种有用的快捷方式,用于创建忽略字符串大小写的 Hashtable 类的新实例。但是,因为 CollectionsUtil.CreateCaseInsensitiveHashTable 方法的所有重载都使用 Thread.CurrentCulture 属性,因而都是区分区域性的。您无法使用此方法创建不区分区域性的 Hashtable。要创建不区分区域性的 Hashtable,请使用接受 culture 参数的 Hashtable 构造函数。对于 culture 参数,请指定 CultureInfo.InvariantCulture。下面的代码示例说明了不区分区域性的 Hashtable 的构造函数。

Visual Basic 复制代码
internalHashtable = New Hashtable(New
CaseInsensitiveHashCodeProvider(CultureInfo.InvariantCulture),
New CaseInsensitiveComparer(CultureInfo.InvariantCulture))


C# 复制代码
internalHashtable = new Hashtable(new CaseInsensitiveHashCodeProvider
(CultureInfo.InvariantCulture),
new CaseInsensitiveComparer(CultureInfo.InvariantCulture));

whycom 2008-04-27
  • 打赏
  • 举报
回复
ms-help://MS.MSDNQTR.v80.chs/MS.MSDN.v80/MS.VisualStudio.v80.chs/dv_fxwalkthrough/html/5cdc9396-a64b-4615-a1cd-b605db4c5983.htm
兔子-顾问 2008-04-27
  • 打赏
  • 举报
回复
如果需要。稍微修改一下就可以用正则来分析了。
兔子-顾问 2008-04-27
  • 打赏
  • 举报
回复
抄下来那个类,然后查找就用
MyMatch.MatchCondition = "abcd";
string findString = Array.Find(strArray, MyMatch.MyMatchFun);
这两行就可以了。
兔子-顾问 2008-04-27
  • 打赏
  • 举报
回复
using System;
using System.Collections.Generic;
using System.Text;

namespace Test
{
class Program
{
public static class MyMatch
{
private static string _matchCondition = string.Empty;
public static string MatchCondition
{
get { return _matchCondition; }
set { _matchCondition = value.ToLower(); }
}

public static bool MyMatchFun(string str)
{
return str.ToLower().IndexOf(MatchCondition) != -1;
}
}

static void Main(/*string[] args*/)
{
string[] strArray = new string[]
{
"Test123",
"test123",
"Abcd",
"AdeF"
};

MyMatch.MatchCondition = "abcd";
string findString = Array.Find(strArray, MyMatch.MyMatchFun);
Console.WriteLine(findString);
Console.ReadKey();
return;
}
}
}


这样算不算简单?
Kevin_LiuFeng 2008-04-27
  • 打赏
  • 举报
回复
同意楼上的。用ACISS码对比。
长弓大侠 2008-04-27
  • 打赏
  • 举报
回复
有两种方法
1 把集合字符统一起来,再做较,要么全大写,要么全小写

2 取ACISS 码的值,做对比吧,可以算出它们的大小写
dancingbit 2008-04-27
  • 打赏
  • 举报
回复
可以不继承,直接使用Hashtable,但是你自己在Add的时候要把key都转换为大写,然后使用索引器的时候也转换为大写。

110,524

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧