Alex Rivera | Logout

Split string to List<string> with Linq

Asked 2010-09-29T12:04:29.057
12

I'd like avoid loop

I have this :

string s = "AAAA,12,BBBB,34,CCCCC,56";

With Linq, I'd like to have 2 List

In the first : AAAA, BBBB and CCCCC

In the second : 12,34 and 56

It's not based on numeric or not numeric.

Thanks,

Edit
Report

1 Answer

6

Given that the rule is that you want every second string in one list and the others in another list, you can do something like this:

        string s = "AAAA,12,BBBB,34,CCCCC,56";

        var parts = s.Split(',');

        var first = parts.Where((p, i) => i % 2 == 0);
        var second = parts.Where((p, i) => i % 2 == 1);
answered 2010-09-29T12:15:36.663

Your Answer