首页新闻找找看学习计划

SOS!!!字符串处理

0
悬赏园豆:15 [已解决问题] 解决于 2008-09-22 10:05

例如有一字符串 A= ac,abc,ac,ac,bc,bc

怎么样处理使得变成 A=ac,abc,bc???

问题补充: 谢谢大家支持 我忘记了一个条件: 就是在存储过程中写这个方法,得到答案A=ac,abc,bc 请大家多帮忙,谢谢!!! 俺实在是不好意思:: 陛下老哥 貌似有点复杂 我要把问题补充完毕- -! 有2个表 表1 表2 表1 中字段 ID (表2)Ids ... 1 1,2,3 ... 2 2,3 ... 表2中字段 Id name kind ... 1 天 .net 2 地 JAVA 3 人 JAVA 写存储过程,得到以下结果: ID (表2)Ids kind .... 1 1,2,3 .net,JAVA ..... 2 2,3 JAVA .... 陛下老哥 辛苦你一下
冷眸的主页 冷眸 | 初学一级 | 园豆:30
提问于:2008-09-19 15:52
< >
分享
最佳答案
0
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

只能说明算法,细节部分有待你自己处理。好好学存储过程吧。

陛下 | 老鸟四级 |园豆:3938 | 2008-09-19 16:09
其他回答(5)
0

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>

roboth | 园豆:28 (初学一级) | 2008-09-19 16:14
0

陛下,你怎么知道他是去掉重复字符呢?

三个问号什么意思啊?

zjy | 园豆:3194 (老鸟四级) | 2008-09-19 16:17
0

            NameValueCollection name = new NameValueCollection();

            foreach (string s in strArr)
            {
                name[s] = s;
            }

            return name.AllKeys;

wingoo | 园豆:1513 (小虾三级) | 2008-09-19 16:19
0

这样看起来是不是简单一些:

string A = "ac,abc,ac,ac,bc,bc";
string B = string.Join(",", A.Split(',').Distinct().ToArray());

只是需要 Linq 支持

丁学 | 园豆:18530 (专家六级) | 2008-09-19 16:22
0

存储过程?死人了……

Gray Zhang | 园豆:17610 (专家六级) | 2008-09-19 16:55
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册