我有一个字符串,如下
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,名字”这两个一组,
如果是中间全部相同,则为一组 哪位大侠帮帮忙,纠结了很久没弄出来。
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,名字
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; }
}
貌似没有什么简单的算法。