Reputation: 367
Hi i started to work with linq and i have some trouble to split an string:
This is the string:
a;b;cod1;xx|a;b;cod2;xx|a;b;cod3;xx|a;b;cod4;xx
first i split the sring by the '|' character so i got this result
a;b;cod1;xx
a;b;cod2;xx
a;b;cod3;xx
a;b;cod4;xx
and then i split again by the ';' in the second index to have this result in to a list
cod1
cod2
cod3
cod4
so there is a better way to do this using linq ¿?
Thanks!
Upvotes: 0
Views: 2859
Reputation: 101052
You can use Select
to put your code into one line (but you still have to use String.Split
):
var s = "a;b;cod1;xx|a;b;cod2;xx|a;b;cod3;xx|a;b;cod4;xx";
var resultlist = s.Split('|').Select(x => x.Split(';')[2]).ToList();
so you don't have to use for
/foreach
loops.
Upvotes: 1