2010-12-10 94 views
6

我有conatin 5號像C#字符串分割和組合

'1,4,14,32,47' 

我想從這個字符串進行5串4號字符串中的每一個

,如:

'1,4,14,32' 
'1,4,14,47' 
'1,4,32,47' 
'1,14,32,47' 
'4,14,32,47' 

什麼是簡單/快速的方法做到這一點

是將其轉換爲數組每次未設置的方式不同的entery和結合 他們回到字符串?

有沒有簡單的方法來做到這一點?

感謝

+0

我懷疑你的第4排有一個類型。應該是'1,14,32,47'而不是'1,4,32,47' – 2010-12-10 08:35:38

+0

坦克我修好了吧 – 2010-12-10 08:42:16

回答

6

如何像

string s = "1,4,14,32,47"; 
string r = String.Join(",", s.Split(',').Where((x, index) => index != 1).ToArray()); 
+0

你是不是指`... index!= x ...`? – 2010-12-10 08:34:55

1

使用string.Split()你可以創建一個字符串數組。循環遍歷它,以便在每次循環迭代中指示應該跳過哪個元素(在第一遍時忽略第一個元素,在第二遍時忽略第二個元素)。

在該循環中,創建一個新數組,其中包含除跳過的所有元素,然後使用string.Join()創建每個結果。

1

看一看:

http://msdn.microsoft.com/en-us/magazine/ee310028.aspx在這裏,你會發現在F#誰給的組合和排列正確的背景爲例(這是怎麼叫你的需要)。有代碼也一樣,我覺得它很容易在C#轉換

Example of Code in C# (text in Italian but code in English)

+0

非常好! +1用於識別他實際上是在嘗試生成字符串中字符的排列。 – 2010-12-10 08:39:14

+1

這很清楚,隊友:D – Mauro 2010-12-10 08:39:49

-1

你的措辭是非常混亂...但這個例子是非常明顯的。只是split它在逗號,然後remove一個索引,然後使用string.Join(「,」,列表);把它重新走到一起..

1

對於那些誰需要一個更通用的算法,這裏是一個賦予的m個n個長度爲子集:

private void GetPermutations() 
{ 
    int permutationLength = 4; 
    string s = "1,4,14,32,47"; 
    string[] subS = s.Split(','); 
    int[] indexS = new int[permutationLength]; 

    List<string> result = new List<string>(); 
    IterateNextPerm(permutationLength, indexS, subS, result); 

    // Result will hold all your genberated data. 
} 

private void IterateNextPerm(int permutationLength, int[] pIndexes, string[] subS, List<string> result) 
{ 
    int maxIndexValue = subS.Count() - 1; 

    bool isCorrect = true; 
    for (int index = 0; index < permutationLength - 1; index++) 
    { 
     if (pIndexes[index] >= pIndexes[index + 1]) 
     { 
      isCorrect = false; 
      break; 
     } 
    } 

    // Print result if correct 
    if (isCorrect) 
    { 
     string strNewPermutation = string.Empty; 
     for (int index = 0; index < permutationLength; index++) 
     { 
      strNewPermutation += subS[pIndexes[index]] + ","; 
     } 
     result.Add(strNewPermutation.TrimEnd(',')); 
    } 

    // Increase last index position 
    pIndexes[permutationLength - 1]++; 

    // Check and fix if it's out of bounds 
    if (pIndexes[permutationLength - 1] > maxIndexValue) 
    { 
     int? lastIndexIncreased = null; 

     // Step backwards and increase indexes 
     for (int index = permutationLength - 1; index > 0; index--) 
     { 
      if (pIndexes[index] > maxIndexValue) 
      { 
       pIndexes[index - 1]++; 
       lastIndexIncreased = index - 1; 
      } 
     } 

     // Normalize indexes array, to prevent unnecessary steps 
     if (lastIndexIncreased != null) 
     { 
      for (int index = (int)lastIndexIncreased + 1; index <= permutationLength - 1; index++) 
      { 
       if (pIndexes[index - 1] + 1 <= maxIndexValue) 
       { 
        pIndexes[index] = pIndexes[index - 1] + 1; 
       } 
       else 
       { 
        pIndexes[index] = maxIndexValue; 
       } 
      } 
     } 
    } 

    if (pIndexes[0] < maxIndexValue) 
    { 
     IterateNextPerm(permutationLength, pIndexes, subS, result); 
    } 
} 

我知道這是不是最好的編碼,但我現在在過去的半小時內已經寫了,所以我確信有些事情需要在那裏進行。

玩得開心編碼!

1
var elements = string.Split(','); 

var result = 
Enumerable.Range(0,elements.Length) 
.Reverse() 
.Select(
i=> 
    string.Join("," 
    Enumerable.Range(0,i).Concat(Enumerable.Range(i+1,elements.Length - i - 1)) 
    .Select(j=>elements[j]).ToArray() // This .ToArray() is not needed in NET 4 
) 
).ToArray();