Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to split a string using LINQ

Tags:

c#

linq

I am using a string to store country code with its cost centre code value . I want to spilit it out the string using LINQ query by | and ; characters. The srting is

IND|001;TWN|002;USA|003;LDN|;MYS|005;

Please help me to spilt out the string value using LINQ

like image 735
Suryakavitha Avatar asked Apr 14 '14 06:04

Suryakavitha


1 Answers

I am assuming you need a list of Tuple<string,string> as output.

var myString = "IND|001;TWN|002;USA|003;LDN|;MYS|005;";
var objects = myString.Split(';')
        .Where(x => !string.IsNullOrEmpty(x))
        .Select (x => x.Split('|'))
        .Select (x => Tuple.Create(x[0],x[1]))
        .ToList();

Result:

IND 001 
TWN 002 
USA 003 
LDN
MYS 005
like image 110
DarkWanderer Avatar answered Oct 15 '22 13:10

DarkWanderer