我有一串项目 - 字符串项目=" a,b,c + 1,2,3,4 + z,x&#34 ;;
我将此字符串拆分为+并添加到N个列表中 List keysList = item.Split(' +')。ToList()
从这个列表中我希望创建密钥
a1z
A1X
A2Z
A2X
A3Z
a3x等: -
我试过下面的代码块。但这里的事情不正确
private string GetValue(List<string> keysList, int i, int keyCount, string mainKey)
{
List<string> tempList = keysList[i].Split(',').ToList();
foreach (string key in tempList)
{
mainKey += key;
i++;
if (i == keyCount)
{
return mainKey;
}
GetValue(keysList, i, keyCount, mainKey);
}
return mainKey;
}
答案 0 :(得分:1)
您可以按如下方式创建帮助函数:
static IEnumerable<string> EnumerateKeys(string[][] parts)
{
return EnumerateKeys(parts, string.Empty, 0);
}
static IEnumerable<string> EnumerateKeys(string[][] parts, string parent, int index)
{
if (index == parts.Length - 1)
for (int col = 0; col < parts[index].Length; col++)
yield return parent + parts[index][col];
else
for (int col = 0; col < parts[index].Length; col++)
foreach (string key in EnumerateKeys(parts, parent + parts[index][col], index + 1))
yield return key;
}
用法演示:
string[][] parts = {
new[] { "a", "b", "c" },
new[] { "1", "2", "3", "4" },
new[] { "z", "x" }
};
foreach (string key in EnumerateKeys(parts))
Console.WriteLine(key);
答案 1 :(得分:1)
这是一种方法......
string item = "a,b,c+1,2,3,4+z,x";
var lists = item.Split('+').Select(i => i.Split(',')).ToList();
IEnumerable<string> keys = null;
foreach (var list in lists)
{
keys = (keys == null) ?
list :
keys.SelectMany(k => list.Select(l => k + l));
}