字符串分割ASP.NET/C#

我处理CSV文件字符串分割" title="字符串分割">字符串分割ASP.NET/C#

ABC|06|001 

PPP|06|001

我正在LINQ分裂CSV

var path = Server.MapPath("~/App_Data/CSV.txt"); 

var _collectCSGData = from line in File.ReadAllLines(path)

let parts = line.Split('|')

select new { ID = parts[0],Assignment=parts[1]};

如何获得每个最后一个项目线?

(即)

001 

001

回答:

from line in File.ReadAllLines(path) 

select line.Split('|').LastOrDefault()

回答:

喜欢的东西:

parts[parts.length -1] 

应该做的伎俩。

回答:

var _collectCSGData = from line in File.ReadAllLines(path) 

let parts = line.Split('|')

let assignment = parts[parts.length - 1]

select assignment;

这应该工作,如果你需要按摩数据,let是你的朋友。

UPDATE:

由于部分可能是空的,你可以有:

let assignment = parts.length > 0 ? parts[parts.length - 1] : String.Empty 

回答:

如果你知道这是第三部分,如何添加到您的匿名构造函数:

var _collectCSGData = from line in File.ReadAllLines(path) 

let parts = line.Split('|');

select new

{ID = parts[0], Assignment = parts[1], Data = parts[2]};

或者,如果它只是“最后一个项目,无论多少项目”

var _collectCSGData = from line in File.ReadAllLines(path) 

let parts = line.Split('|');

select new

{ID = parts[0], Assignment = parts[1], Data = parts[part.length-1]};

以上是 字符串分割ASP.NET/C# 的全部内容, 来源链接: utcz.com/qa/265362.html

回到顶部