Searching a text file

Bigbadborris

Active member
Joined
Aug 21, 2013
Messages
35
Programming Experience
Beginner
Hi

I have a text file which holds customers details. One customer per line. Each line os formated as below

GRAHAM ### ##### ############# ################ ##############
MANSFIELD ### ##### ################## ############## ########
(i have had to mask some of the data for security)

I would like to no how to remove the first 20 Characters from each line,( The first 20 characters of each line are blank spaces ) and then search the first 3 characters of the Name feild (GRA in above example) based on the characters the user enters in a textbox. I would like the program to then display all lines where the first 3 characters of the name field match the search string in a lst box (lstCust).

e.g User enters GRA, the program then displays all the lines where the first 3 letters of the name match in the lst box

I hope this makes sence

Many Thanks
 
Ok Ive Managed to get some result with this.


VB.NET:
' Displays an Input box for the user to enter the search string
        SearchText = InputBox("Please Enter Customers Name", "Search Customer", vbYesNoCancel)

        ' Opens the Text file and searches line by line for the search string then writes the lines containing
        ' the require search string to another txt file and displays them in a list box.
        Dim readFile As String = "E:\Programming\Data Files\CUSTKEY.txt"
        Dim outputFile As String = "E:\Programming\Data Files\CUSTKEY2.txt"
        Dim strSearch As String = "                    " & SearchText
        Dim lines As New List(Of String)
        Using reader As StreamReader = New StreamReader(readFile)
            Do Until reader.EndOfStream
                lines.Add(reader.ReadLine)
            Loop
            reader.Close()
        End Using
        Dim selectedlines = From line In lines Where line IsNot Nothing AndAlso line.StartsWith(strSearch) Select line
        Using objWriter As StreamWriter = New StreamWriter(outputFile, True)
            For Each line In selectedlines
                objWriter.WriteLine(line)
                lstCust.Items.Add(line)
            Next
            objWriter.Close()
        End Using

This displays the lines which match the search string in a listbox.

I now would like the user to click on the correct record/line in lstcust then i need the program to start at position 20 along the line then read for 30 characters and display everything between position 20 and 50 into textbox1.

I will try and figure this out myself but I am getting to the limits of what I have learned so far. So all help would be much appreciated.

Many thanks.
 
Hi,

Your best option would be to call the TrimStart method on each line as it is read by the Streamreader to remove the leading spaces in each record. That way you would not need to create that search variable that you are using and you would not need to factor this in when wanting to get the information to your TextBox. i.e:-

VB.NET:
lines.Add(reader.ReadLine.TrimStart)

Have a look here for further information:-

String.TrimStart Method (System)

Hope that helps.

Cheers,

Ian
 
Back
Top