剔除string[]中重复字符串
一string[] t
比如有10条记录
其中不确定有重复记录
如何剔除重复记录?
[解决办法]
try
List <string> list = new List <string> ();
string[] temp = { "abc ", "dd ", "ss ", "abc ", "ss ", "ee ", "xx " };
foreach (string s in temp)
{
if (!list.Contains(s))
{
list.Add(s);
}
}
string[] result = list.ToArray();
[解决办法]
插入到Hashtable,让键跟值一样不就行了...
但性能怎么样就不敢说了~
[解决办法]
List <string> list = new List <string> ();==> 一个集合,类型为string
[解决办法]
泛型是2.0新增的,如果是1.1用ArrayList
ArrayList list = new ArrayList();
string[] temp = { "abc ", "dd ", "ss ", "abc ", "ss ", "ee ", "xx " };
foreach (string s in temp)
{
if (!list.Contains(s))
{
list.Add(s);
}
}
[解决办法]
如果是从数据库中读取,可以在查询语句处踢出。
用distinct或having子句查询即可
[解决办法]
几个高人周末也不休息啊
[解决办法]
Hashtable ht = new Hashtable();
foreach (string s in stringarray)
{
ht[s] = "1 ";
}
string[] newstring = (string[])ht.Keys;