如何使用LINQ分割字符串 [英] How to split a string using LINQ

查看:470
本文介绍了如何使用LINQ分割字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个字符串来存储国家/地区代码及其成本中心代码值.我想使用LINQ查询将其拆分为字符串.和 ;人物. srting是

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;

请帮助我使用LINQ溢出字符串值

Please help me to spilt out the string value using LINQ

推荐答案

我假设您需要一个Tuple<string,string>列表作为输出.

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

结果:

IND 001 
TWN 002 
USA 003 
LDN
MYS 005

这篇关于如何使用LINQ分割字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆