Question Need help with fixed width string parsing

mattman-sd

New member
Joined
Aug 5, 2011
Messages
4
Programming Experience
1-3
Using .net 4
I have files that are one long line of text but are made up of individual segments. I have managed to parse the segments into a string array but now need to parse each segment into its individual fields, based upon a field structure defined by the first 2 characters of the segment.
For example:
CA12345ABC999999XXPPPPPPPP55
CA segment (CA is first 2 characters of segment) has fields defined as:
Field Name,Start,Length
Field1,3,5 (value shown is 12345)
Field2,8,3 (value shown is ABC)
Field3,11,6 (value shown is 999999)
Field4,17,2 (value shown is XX)
Field5,19,8 (value shown is PPPPPPPP)
Field6,27,2 (value shown is 55)
There are about 130 different segments each with its own field structure.
I want to define some structure which contains the segment/field structure so I can the parse each segment based upon the first 2 characters of the segment.
I tried using Microsoft.VisualBasic.FileIO.FieldType.FixedWidth but that is used for fixed width files only where every line is the same.
Anybody got any ideas?
Thanks
 
TextFieldParser can be used, use PeekChars method to get the type of line and set FieldWidths conditionally as the file is read.
 
I am trying to parse a string, not a file.
can you give a code example where the string is called data and the field widths are 5,8,3,20

Thanks
 
You can still use TextFieldParser, it's source can be for example a TextReader, which can be a StringReader loaded from a string.
Also, if you want you can make a long string of the segments separated by linefeeds (instead of the array), and use that as source for single instances of StringReader and TextFieldParser.
 
Back
Top