user3340949 user3340949 - 6 months ago 34 Question

VB Read delimited text file array

I have a text file delimited by pipes. I want to read the value at the fifth pipe but I cannot figure out how to do that. All I can do is read each section of the array. Can't find examples on this.
EPD|TR2999-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENEDCOMPONENTS|EPP|Buy|6.237|916.839|147||181|||CCACOE||PS.777.||150||

EPD|TR2309-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|193.347|31||181|||777||PS.777.||150||


This example is using a text file with these two lines in it:

Line1: EPD|TR2999-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|916.839|147||181|||CCACOE||PS.777.|‌​|150||

Line2: EPD|TR2309-01G|SEMI, TRANS, P-CH, SEL|ACTIVE|PS.COE.6|SCS|SCREENED COMPONENTS|EPP|Buy|6.237|193.347|31||181|||777||PS.777.||150‌​||

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim fp as string = "" 'enter the full path to your file here
    Dim value as string = GetValueForPart(fp, Me.TextBox1.Text)
    MsgBox(value) 'in this example, value is set to "6.237" when textbox input is "TR2999-01G"
End Sub

Private Function GetValueForPart(ByVal filepath As String, ByVal SearchPartNum As String) As String
    If Not File.Exists(filepath) Then Return Nothing
    If SearchPartNum Is Nothing OrElse SearchPartNum.Trim = "" Then Return Nothing
    Dim ret As String = Nothing
    Using sr As New StreamReader(filepath)
        Do While sr.Peek >= 0
            Dim line() As String = sr.ReadLine.Split(CChar("|"))
            If line IsNot Nothing AndAlso line.Count >= 5 Then
                If line(1).Equals(SearchPartNum) Then
                    ret = line(9)
                    Exit Do
                End If
            End If
    End Using
    Return ret
End Function

I just tested this, all you need to do is enter your full filepath on the second line