VB读取分​​隔的文本文件数组

时间:2016-09-30 14:10:57

标签: vb.net visual-studio

我有一个由管道分隔的文本文件。我想读取第五个管道的值,但我无法弄清楚如何做到这一点。我所能做的就是读取数组的每个部分。无法找到相关的例子。

    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||

1 个答案:

答案 0 :(得分:0)

此示例使用包含以下两行的文本文件:

  

第1行: EPD | TR2999-01G | SEMI,TRANS,P-CH,SEL | ACTIVE | PS.COE.6 | SCS | SCREENED COMPONENTS | EPP |购买| 6.237 | 916.839 | 147 || ||| 181 || CCACOE PS.777 |。| 150 ||

     

第2行: EPD | TR2309-01G | SEMI,TRANS,P-CH,SEL | ACTIVE | PS.COE.6 | SCS | SCREENED COMPONENTS | EPP |购买| 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
        Loop
    End Using
    Return ret
End Function

我刚试过这个,你需要做的就是在第二行输入完整的文件路径