0

I have a .txt file which looks like this

Image

So as you can see there is no specific delimiter. Same number of spaces exist after the first column however all names have different lengths because of which the number of spaces in the next column is varying.

I thought of adding a for loop iterating through a name and it'll increment the value of a variable whenever it detects a space. And as soon as I get two spaces then it'll move to the next column. But this solution won't work because some people have middle names. And sometimes their name might be extremely long and it'll fill up the entire column.

So what algorithm should I use to solve this issue. I'm coding on C# btw.

Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Its called a "fixed width file" - and in years gone by they were all the rage (think of it as pre-Xml Xml). There will be many existing parsers able to cope with this format given its specification – Jamiec May 20 '16 at 15:40
  • @Jamiec Any links or suggestions on how to code it? –  May 20 '16 at 15:43
  • Essentially you have 4 values/fields per line. You will be able to extract substrings using fixed indexing and then trim the whitespace from the right of each substring. As respects the name component and splitting that down into smaller values like first name/last name, that is more challenging. – Andrew Truckle May 20 '16 at 15:43

1 Answers1

0

What you have there is a fixed width file. It should come with a specification which details what the width of each field is.

Therefore, parsing it is simply as easy as enumerating each row taking the right number of characters and trimming any white space from each field.

So for example, if you know that your 4 fields are of length 20,25,10,20, given a row row you could do

var field1 = row.Substring(0,20).Trim();
var field2 = row.Substring(20,25).Trim();
var field3 = row.Substring(45,10).Trim();
var field4 = row.Substring(55,20).Trim();

There is more info here

Read fixed width record from text file

https://codereview.stackexchange.com/questions/27782/how-to-read-fixed-width-data-fields-in-net

Community
  • 1
  • 1
Jamiec
  • 133,658
  • 13
  • 134
  • 193