Click here to Skip to main content
15,886,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to parse flat text file (without delimiter) using the list of index values (Start Index, End Index).

For e.g. Flat File content

123 Name1 Address1 Class1 Grade1 ....
456 Name2 Address1 Class2 Grade2 ....
789 Name3 Address1 Class3 Grade3 ....
.....
.....

Now, I've dictionary object which has key pair values of each of the fields. (populated from Backend/config. I'm using dictionary o/w we can use array of array, list as well)

<key, value[startindex,endindex])

<id,[0,5]="">
<name,[6,17]>
<address,[18,35]>
<class,[36,43]>
<grade,[44,50]>

My Student Class having 5 properties : Id, Name, Address, Class, Grade
(My Dictionary Key and Class Property Names are Same)

Now, I want to process the file line by line, get all 5 property values populated from each line and store into my Student class Object. Thus, end up having list of student list (in above ex. - list will have 3 items at end of the process)

Perhaps we can use TextFieldParser and foreach loop but in real time my file will have ~40-50 attributes and No. of line can be 10 to 1000+.
Hence, I'm looking for efficient way to parse each line, extract value for each field using index values, create object out of it.

Thanks for your help.

What I have tried:

TextFieldParser and ForEach loop to read file line by line extract values using start/end index.
Posted
Updated 26-Jul-21 5:01am
Comments
PIEBALDconsult 26-Jul-21 10:31am    
Unless you can use SSIS or similar, I doubt you can improve over the TextFieldParser, just go with it.
Member 2726194 26-Jul-21 10:38am    
Hi, Thanks. I'm not using SSIS. I'm using C# Core5

1 solution

If the TextFieldParser isn't what you want, then you can just use string.Substring: it has an overload that extract a string of length N starting at offset O: String.Substring Method (System) | Microsoft Docs[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900