searching text file

dinotom

Member
Joined
Sep 5, 2011
Messages
11
Programming Experience
3-5
in vba i would use vlookup on an excel table.
How do you search a text file in vb.net that is formatted like this
ES,2
C,3
EC,4

I want to search for the two letter string and retrieve the integer that follows the comma
 
well here is how I did it, any suggestions on improvement?


Function ReadCommodCodeTextFile(ByRef searchKey As String) As Integer Dim i As Integer Dim searchKeyUpper As String = searchKey.ToUpper() Using reader = New StreamReader("X:\QueryTxtFiles\RoundingIntegersComma.txt") Dim line As String = reader.ReadLine() Do While line IsNot Nothing If line.Contains(searchKeyUpper) Then i = Convert.ToInt32(Replace(line, searchKeyUpper & ",", "")) Exit Do End If line = reader.ReadLine() Loop End Using Return i End Function
 
simpler version of above, hopefully with code tags

Function ReadCommodCodeTextFile (ByRef searchKey As String) As Integer Dim i As Integer Using reader = New StreamReader ("X:\QueryTxtFiles\RoundingIntegersComma.txt") Dim line As String = reader.ReadLine() Do While line IsNot Nothing If line.Contains (searchKey.ToUpper()) Then i = Convert.ToInt32 (Replace (line, searchKey.ToUpper() & ",", "")) Exit Do End If line = reader.ReadLine() Loop End Using Return i End Function
 
Back
Top