Read text file with variable field widths

I am using Visual Studio 2005 (VB.Net) to write my app. At some point the app. need to read a text file with data in columner format. The problem I have is that the columns are of variable width (not fixed). Is there a way that I can use white space characters to distinguise between different fields in a row?

The file is not comma or tab delimited, but it has spaces between fields. Spaces are present only between individual fields, never in the fields themselves.

Below is a couple of lines from the file.

1 PH5 902.0 MG 2 T1ORE 1 ORE1 2000 ALL 0.300

1 PH5 902.0 HB% 7 WS101 101 WST1 2000 ALL 0.600

1 PH9 1172.0 FD% 4 WS101 101 WST1 2000 ALL 0.500

Thanks

[707 byte] By [ChristieMyburgh] at [2007-12-26]
# 1
Read the text with StreamReader.ReadLine(), then parse the fields in each line with a function like this:

Private Function ParseFields(ByVal s As String) As String()
Dim flds(10) As String
Dim cnt As Integer = 0
Dim ix As Integer = 0
Do While ix < s.Length and cnt <= UBound(flds)
' Find trailing white space
Dim jx As Integer = s.IndexOf(" ", ix)
If jx < 0 Then jx = s.Length
flds(cnt) = s.Substring(ix, jx - ix)
cnt = cnt + 1
' Find next field
Do While jx < s.Length AndAlso s.Substring(jx, 1) = " "
jx = jx + 1
Loop
ix = jx
Loop
Return flds
End Function

nobugz at 2007-9-4 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 2
thanks, will try and get back to you
ChristieMyburgh at 2007-9-4 > top of Msdn Tech,.NET Development,Common Language Runtime...
# 3
Tried it, and it works a treat...thanks
ChristieMyburgh at 2007-9-4 > top of Msdn Tech,.NET Development,Common Language Runtime...

.NET Development

Site Classified