【发布时间】:2016-10-17 13:54:20
【问题描述】:
我有这段代码用于在文本文件中查找一个值,然后在同一行中读取另一个值。此后,我从正在阅读的大型文本文件中发现,可以有两行或多行带有我要搜索的初始值,即零件号。找到零件号后,我得到了数量。如何修改它以找到 ALL 的零件编号行,然后从该行返回每个值?
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
【问题讨论】:
标签: vb.net