例如有一字符串 A= ac,abc,ac,ac,bc,bc
怎么样处理使得变成 A=ac,abc,bc???
Code
string A = "ac,abc,ac,ac,bc,bc";
string[] temp = A.Split(',');
List<string> t = new List<string>();
foreach (string s in temp) {
if (!t.Contains(s))
t.Add(s);
}
string[] tStr = new string[t.Count];
t.CopyTo(tStr);
A = string.Join(',', tStr);
天啊!
小兄弟用存储过程也不说明,害的以为是杀猪呢还是杀驴:)(用貌似最简单的考验咱哥们)
张老说了,死人了,未必,哈哈。
Sql Split
ArrayDistinct
ALTER PROCEDURE dbo.ArrayDistinct(@Source varchar(255),@SplitChar char)
AS
BEGIN
SELECT * INTO #T FROM Split(@Source, @SplitChar)
SELECT DISTINCT * FROM #T
END
经测试输入 a,b,c,a,b
运行[dbo].[ArrayDistinct] ( @Source = a,b,c,a,b, @SplitChar = , ).
Word
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
a
b
c
(5 行受影响)
(返回 3 行)
@RETURN_VALUE = 0
完成 [dbo].[ArrayDistinct] 运行。
搞定!
:(,自恃晚辈,无敌感情牌,终是不忍啊。下不为例。
你的需求应该早些说明,不幸的指明,你的实现方法估计是走错方向了,完全不需要针对 Ids 做 Disctinct 操作!
SAMPLE
CREATE TABLE #T(Index INT AUTO, Id INT, Ids VARCHAR(255));
SELECT Id, Ids INTO #T(Id, Ids) FROM T1;
DECLARE @Index INT, @Count INT, @Temp INT;
SELECT COUNT(Index) INTO @Count FROM #T;
CREATE TABLE #R(Id INT, Ids INT, Kind INT);
SET @Index = 1;
WHILE(@Index <= @Count)
BEGIN
SELECT Ids INTO @Temp FROM #T WHERE Index = @Index;
SELECT @Index AS Id, @Temp AS Ids, DISTINCT Kind INTO #R(Id, Ids, Kind) FROM T2 WHERE Id IN(@Temp)
@Index ++;
END
SELECT * FROM #R -- return
只能说明算法,细节部分有待你自己处理。好好学存储过程吧。
ArrayList,List<>,Hashtable含有Contains这样的方法。
实现起来不难吧 。。
给你个js的
<script language="JavaScript">
var array1=new Array("a","c","b","b","a","c","c","c");
Array.prototype.unique = array_unique;
function array_unique()
{
var o = new Object();
for (var i=0,j=0; i<this.length; i++)
{
if (typeof o[this[i]] == 'undefined')
{
o[this[i]] = j++;
}
}
this.length = 0;
for (var key in o)
{
this[o[key]] = key;
}
return this;
}
var d = new Date().getTime();
document.write(array1.unique());
d = new Date().getTime()-d;
document.write("<br>2000节点 新算法计耗时 "+ d +" 毫秒!");
</script>
NameValueCollection name = new NameValueCollection();
foreach (string s in strArr)
{
name[s] = s;
}
return name.AllKeys;
这样看起来是不是简单一些:
string A = "ac,abc,ac,ac,bc,bc";
string B = string.Join(",", A.Split(',').Distinct().ToArray());
只是需要 Linq 支持
存储过程?死人了……