Using ReadFields() function, retrieve n-th field value

2k views Asked by At

I am currently reading a text file that has a fixed width. Instead of looping, is there a way to get let's say the fifth value (which is one character if you look at the fixedWidths)?

Here is a copy of my code:

Private Function processPaymentRow(currentLine As String)
  Dim result As String()

  Using strStream As New StringReader(currentLine)
    Using MyReader As New TextFieldParser(strStream)
      MyReader.TextFieldType = FieldType.FixedWidth
      'Set proper field widths for the payment row here
      MyReader.FieldWidths = {10, 1, 10, 8, 1, 20, 13, 1, 8, 8, 8, 40,
                              40, 40, 40, 40, 25, 2, 9, 40, 10, 20, 6}
      Try
        result = MyReader.ReadFields()
        'Dim currentField As String
      Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
        MsgBox("Line " & ex.Message & "is not valid and will be skipped.")
      End Try
    End Using
  End Using
  For Each itm In result
    MsgBox(itm & " Pay")
    '// I can loop through the results and get the value I want.
  Next
  Return result
End Function
1

There are 1 answers

1
Victor Zakharov On BEST ANSWER

result is a String array, so you should be able to grab result(4), and that should give you your 5th value.