Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# string split and combine

i have a string that conatin 5 numbers like

'1,4,14,32,47'

i want to make from this string 5 strings of 4 number in each one

like :

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

what is the simple/faster way to do it

is the way of convert this to array unset every time diffrent entery and combine them back to string ?

is there a simple way to do it ?

thanks

like image 766
Haim Evgi Avatar asked Dec 10 '10 08:12

Haim Evgi


2 Answers

How about something like

string s = "1,4,14,32,47";
string r = String.Join(",", s.Split(',').Where((x, index) => index != 1).ToArray());
like image 172
Adriaan Stander Avatar answered Sep 30 '22 01:09

Adriaan Stander


Using string.Split() you can create a string array. Loop through it, so that in each loop iteration you indicate which element should be skipped (on the first pass, ignore the first element, on the second pass, ignore the second element).

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

like image 42
C.Evenhuis Avatar answered Sep 30 '22 01:09

C.Evenhuis