110,571
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace csdn_1
{
class Program
{
static void Main(string[] args)
{
string needTest = "w,z|a,b,c|z,x|a"; //举个例子;
List<string[]> save=new List<string[]>(); //泛型容器,用于保存下面划分的字符串数组;
string[] s1 = needTest.Split('|'); //先按“|”分割
for (int i = 0; i < s1.Length; i++)
{
string[] s2 = s1[i].Split(',');//再按“,”分割
save.Add(s2);//保存到泛型中去;
}
string[] result=DiGui(save);//利用下面的递归算法;最终得到一个你想要的一维字符串结果~
foreach (string item in result) //查看结果
{
Console.WriteLine(item+"\n");
}
Console.ReadLine();
}
/// <summary>
/// 这个方法的思路是,将划分好的相邻数组里的数据两两相组合,如按“|”划分后:数组1{w,z},数组2{a,b,g},数组3{z}:
/// 先将数组1的各数据和数组2个数据组合,形成:数组1{wa,wb,wg,za,zb,zg},数组2{z}
/// 先将数组1的各数据和数组2个数据组合,形成:{waz,wbz,wgz,zaz,zbz,zgz}
/// </summary>
/// <param name="list"></param>
/// <returns></returns>
public static string[] DiGui(List<string[]> list)
{
int num = 0;
string[] _temp = new string[list[0].Length * list[1].Length];
for (int i = 0; i < list[0].Length; i++)
{
for (int j = 0; j < list[1].Length; j++)
{
_temp[num]=list[0][i]+list[1][j];
num++;
}
}
list.RemoveAt(0);//删除原先的第一个数组;
list[0] = _temp;//第一个数组改成新的组合数组;
if (list.Count > 1)//最后集合中肯定是只剩一个字符创数组了,也就是你要的结果;
{
DiGui(list);
}
return list[0];
}
}
}
var source = "a,b|C,D,E|f,g|H,I";
var resultList = (from unit in source.Split(new char[] { '|' }) select unit.Split(new char[] { ',' })).Aggregate(Enumerable.Repeat("", 1), (result, current) => (from o in result from t in current select o + t)).ToList();