2011-05-31 42 views
0

可能重複:
Get all possible word combinations如何得到一個字符串的動力清單在C#

我希望得到一個字符串的 「權力清單」。因此,考慮此輸入:

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

函數將返回:

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

我怎樣才能做到這一點?

+1

這僅僅是一個讓所有組合的問題... – soandos 2011-05-31 06:13:36

+2

相關:http://stackoverflow.com/questions/4290889/get-all-possible-word-combinations – 2011-05-31 06:22:21

+0

如果輸出給出了空字符串,它可能會更好地構成你使用它的任何內容。更一致。 – luqui 2011-05-31 06:22:50

回答

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); 
} 
+0

它返回120個字符串,我只需要那6個字符串,試試自己 – Ata 2011-05-31 06:58:26

+0

我寫了一個函數 – DeveloperX 2011-05-31 09:41:24

相關問題