Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String Split ASP.NET /C#

Tags:

c#

asp.net

linq

I am processing CSV File

Say

ABC|06|001
PPP|06|001

I am running LINQ to split the 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]};

How to get the last item of each line ?

(i.e)

001
001
like image 582
Gopi Avatar asked Dec 28 '22 08:12

Gopi


2 Answers

from line in File.ReadAllLines(path)
select line.Split('|').LastOrDefault()
like image 102
Jacob Avatar answered Jan 16 '23 10:01

Jacob


Something like:

parts[parts.length -1]

should do the trick.

like image 28
codykrieger Avatar answered Jan 16 '23 11:01

codykrieger