Home > other >  Read line on Txt after specific text VB.NET
Read line on Txt after specific text VB.NET

Time:04-26

I have the following text on my notepad:

Manufacture, a, b, c
Manufacture, h, i, j
Supplier, 7, 8, 9
Manufacture, x, y, z
Supplier, 0,9,5

Then I have a form that contains 2 textbox. 1 is for Manufacture and 2 is for Supplier.
How can I split it? So when I push the read button. The textbox would be:

TextboxManufacture

Manufacture, a, b, c
Manufacture, h, i, j
Manufacture, x, y, z

TextboxSupplier

Supplier, 7,8,9
Supplier, 0,9,5

Currently, I have the following code. But still basic.

Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\test.txt")
MsgBox(fileReader)

CodePudding user response:

This version will read your input file, determine the line ID, and build an array of values. Lastly join the values using a new line and assign into the textbox.

   Private Sub ParseInputFile(filePath As String)

        Dim lines() As String = File.ReadAllLines(filePath)

        Dim manufs As New List(Of String)
        Dim suppliers As New List(Of String)

        For Each lineItem As String In lines

            Dim vals() As String = lineItem.Split(Convert.ToChar(","))

            If vals.Length > 0 Then

                Dim lineId As String = vals(0)
                If lineId = "Manufacture" Then
                    manufs.Add(lineItem)
                ElseIf lineId = "Supplier" Then
                    suppliers.Add(lineItem)
                End If

            End If

        Next

        TextboxManufacture.Text = String.Join(Environment.NewLine, manufs)
        TextboxSupplier.Text = String.Join(Environment.NewLine, suppliers)

    End Sub
  • Related