首页 新闻 会员 周边

C#如何为以下字符串分组

0
悬赏园豆:20 [已解决问题] 解决于 2011-08-05 00:33

我有一个字符串,如下

string abc = "1,15,25,名字|2,0,0,名字|3,15,25,名字|4,15,25,名字|5,10,25,名字|6,0,0,名字|7,15,25,名字|8,15,25,名字";

需求是这样的:像 1,15,25,| 这样的内容,中间15,25有可能为0,或不为0 “|” 号前边的中间两位和后边的中间两位相同,则分在一组, 如果中间两位是零或者其中一个0,则跳过,前边为一组,后边为一组,

也就是说如果是“1,15,25,名字|1,15,25,名字|1,15,25,名字|1,555,25,名字”这个样子的 就前三个一组,后边的一组

如果是“1,15,25,名字|1,0,0,名字|1,0,0,名字|1,15,25,名字|1,15,25,名字”这样样子的 就第一个“|”号前边的一组,“1,15,25,名字|1,15,25,名字”这两个一组,

如果是中间全部相同,则为一组 哪位大侠帮帮忙,纠结了很久没弄出来。

破陋的锅的主页 破陋的锅 | 初学一级 | 园豆:200
提问于:2011-08-02 20:17
< >
分享
最佳答案
0
public static string[] GroupString(string s)
{
string[] a = s.Split('|');//用|分组
List<string> result = new List<string>();
string last = null;
for (int i = 0; i < a.Length; i++)
{
if (last == null)
{
last
= a[i];
result.Add(last);
}
else
{
string[] l = last.Split(',');//用,分组
string[] b = a[i].Split(',');
if (l[1] == b[1] && l[2] == b[2])//比较
{
result[result.Count
- 1] += "|" + a[i];
}
else
{
last
= a[i];
result.Add(last);
}
}
}
return result.ToArray();
}

调用:

string[] array = Code1.GroupString("1,15,25,名字|2,0,0,名字|3,15,25,名字|4,15,25,名字|5,10,25,名字|6,0,0,名字|7,15,25,名字|8,15,25,名字");
foreach (var item in array)
{
Console.WriteLine(item);
}

输出:

1,15,25,名字
2,0,0,名字
3,15,25,名字|4,15,25,名字
5,10,25,名字
6,0,0,名字
7,15,25,名字|8,15,25,名字

收获园豆:10
无泪之魂 | 菜鸟二级 |园豆:287 | 2011-08-03 13:17
其他回答(2)
0

public class StringGroup
{
public void Test()
{
string abc = "1,15,25,名字|2,0,0,名字|3,15,25,名字|4,15,25,名字|5,10,25,名字|6,0,0,名字|7,15,25,名字|8,15,25,名字";
string abc1 = "1,15,25,名字|1,15,25,名字|1,15,25,名字|1,555,25,名字";
string abc2 = "1,15,25,名字|1,0,0,名字|1,0,0,名字|1,15,25,名字|1,15,25,名字";
Show(abc);
Show(abc1);
Show(abc2);
}
public void Show(string abc)
{
Dictionary
<string, List<info>> result=Group(abc);
Console.WriteLine(
"测试的字符串为:"+abc);
foreach (var item in result)
{
Console.WriteLine(
"\nkey:"+item.Key);
foreach (var val in item.Value)
{
Console.WriteLine(val.A
+","+val.B+","+val.C+","+val.Name);
}
Console.WriteLine();
}
Console.WriteLine(
"\n\n");
}

public Dictionary<string, List<info>> Group(string str)
{
Dictionary
<string, List<info>> ret = new Dictionary<string, List<info>>();
List
<info> infoList=new List<info>();
List
<string> abcArray = str.Split('|').ToList();
abcArray.ForEach(
a
=>
{
string[] temp = a.Split(',');
infoList.Add(
new info()
{
A
= Int32.Parse(temp[0]),
B
= Int32.Parse(temp[1]),
C
= Int32.Parse(temp[2]),
Name
= temp[3]
});
}
);
infoList.Add(
new info() {
A
=0,
B
=0,
C
=0,
Name
=""
});

List
<info> infos=new List<info>();
infoList.ForEach(
val
=>
{
if (val.B * val.C != 0)
{
infos.Add(val);
}
else
{
if (infos.Count > 0)
{
var result
= GetValue(infos);
foreach (var value in result)
{
if (ret.Keys.Contains(value.Key))
{
int i=0;
string newkey = value.Key;
while(ret.Keys.Contains(newkey))
{
i
++;
newkey
+= "_"+i;
}
ret.Add(newkey, value.Value);
}
else
{
ret.Add(value.Key, value.Value);
}
}
infos
= new List<info>();
}
}
}
);
return ret;
}

public Dictionary<string,List<info>> GetValue(List<info> infoList)
{
Dictionary
<string, List<info>> ret = new Dictionary<string, List<info>>();
var result
= infoList
.Where(p
=> p.B * p.C != 0)
.GroupBy(q
=>new{q.B,q.C})
.Select(
o
=>new
{
key
=o.Key,
value
=o.Where(s=>s.B==o.Key.B&&s.C==o.Key.C)
}
);
result.ToList().ForEach(
s
=>
{
ret.Add(s.key.B
+ "," + s.key.C, s.value.ToList());
}
);
return ret;
}
}

public class info
{
public int A { get; set; }
public int B { get; set; }
public int C { get; set; }
public string Name { get; set; }
}
最后调用StringGroup的Test方法就OK了,效果见图:

收获园豆:10
artwl | 园豆:16736 (专家六级) | 2011-08-02 21:41
您这整这么复杂好像也不太符合人家的要求诶
支持(0) 反对(0) 无泪之魂 | 园豆:287 (菜鸟二级) | 2011-08-03 13:01
非常感谢二位,这两天没来看,
我已经写完了,就是循环里套了很多的验证条件,
和无泪的相似,但没无泪的方法简洁。
感谢大家的帮助
支持(0) 反对(0) 破陋的锅 | 园豆:200 (初学一级) | 2011-08-05 00:36
0

貌似没有什么简单的算法。

刘鸿海 | 园豆:655 (小虾三级) | 2011-08-03 08:11
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册