'Read line on Txt after specific text VB.NET

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)


Solution 1:[1]

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

Solution 2:[2]

I read your question because it was interesting for me and I knew a different way to get the results, feel completely free to check the code and enjoy with the results that you will get.

Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim oReader As System.IO.StreamReader = Nothing
    Dim str1 As String = ""
    Try
        oReader = New IO.StreamReader(".\data.txt") 'path with the file with the data to be read.
        str1 = oReader.ReadLine 'try to read the first line of the file data.txt
    Catch ex As Exception
        oReader.Close() 'important: in case of error don't forget close the oReader for free the pointer to data.txt.
    End Try
    While Not str1 Is Nothing
        Dim pos As Byte = str1.IndexOf(",") 'first occurence of character comma, from left to right.
        Dim str2 As String = str1.Substring(0, pos) 'string with the first part of the string, (Manufacture, Supplier).
        Dim str3 As String = str1.Substring(pos + 2, str1.Length - 2 - pos) 'string with the second part of the string.
        Select Case str2
            Case "Manufacture"
                TextboxManufacture.Text &= str3 & vbCrLf 'we add to TextboxManufacture the data and a newline character
            Case "Supplier"
                TextboxSupplier.Text &= str3 & vbCrLf 'we add to TextboxSupplier the data and a newline character
        End Select
        str1 = oReader.ReadLine 'read a new line of the file data.txt
    End While
    oReader.Close() 'important: when finished of read lines, it free the pointer to data.txt
End Sub
End Class

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew Mortimer
Solution 2 purple_2022