C # string split and combination

I have a string containing 5 numbers such as

'1,4,14,32,47' 

I want to make from this line 5 lines of 4 numbers in each

like:

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

what is an easy / quick way to do it

Is this a way to convert this array to unset every time a complex process is executed and merged them back into a string?

Is there an easy way to do this?

thanks

+6
string arrays c # combinations
source share
6 answers

How about something like

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

Using string.Split() , you can create an array of strings. Skip it, so in each iteration of the loop you indicate which element should be skipped (on the first pass, ignore the first element, on the second pass, ignore the second element).

Inside this loop, create a new array containing all the elements, but the one you want to skip, then use string.Join() to create each result.

+1
source share

Take a look:

http://msdn.microsoft.com/en-us/magazine/ee310028.aspx Here you will find an example in F # that will give the correct background in combinations and permutations (which is called what you need). There is also code, I think it’s easy to translate it to C #

Sample code in C # (text in Italian, but code in English)

+1
source share

For those who need a more general algorithm, this is what gives n subsets of m elements:

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

I know that this is not the most pleasant encoding, but I wrote it right now in the last half hour, so I'm sure that there is something there that could be developed there.

Have fun coding!

+1
source share
 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(); 
+1
source share

Your wording is rather confusing ... but the example is clear enough. just split by comma, then delete one index, then use string.Join (",", list); bring him back together.

-one
source share

All Articles