如何在C#中获取字符串的动力列表

时间:2011-05-31 06:10:47

标签: c#

  

可能重复:
  Get all possible word combinations

我想得到一个字符串的“强力列表”。所以给出了这个输入:

string[] s = new string[] { "a", "b", "c" } ;

该函数将返回:

string[] s = new string[] { "a", "b", "c", "ab", "ac", "bc", "abc" } ;

我该怎么做?

1 个答案:

答案 0 :(得分:0)

试试这个:

string[] chars = new string[] { "a", "b", "c" };

List<string> result = new List<string>();
foreach (int i in Enumerable.Range(0, 4))
{
    IEnumerable<string> coll = chars;
    foreach (int j in Enumerable.Range(0, i))
    {
        coll = coll.SelectMany(s => chars, (c, r) => c + r);
    }
    result.AddRange(coll);
}