使用ReadFields()函数,检索第n个字段值

时间:2014-01-16 18:27:55

标签: vb.net parsing textfieldparser

我目前正在阅读具有固定宽度的文本文件。而不是循环,有没有办法让我们说第五个值(如果你看一下fixedWidths,这是一个字符)?

以下是我的代码的副本:

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 个答案:

答案 0 :(得分:1)

result是一个String数组,所以你应该能够抓住result(4),这应该会给你第5个值。