Odell
Odell

Reputation: 13

Streamreader - Read Next Line

I am new to VB.NET so forgive me if this it a simple question.

I am trying to read the contents of a text file into a Multiline Textbox using the OpenFileFileDialog Box. I am doing this by doing the following:

' Set Properties of OpenFileDialog
With OpenFileDialog1
    .FileName = ""
    .Title = "Open Text File"
    .InitialDirectory = "c:\"
    .Filter = "Text files|*.txt"
    .ShowDialog()
End With

' Populate Textbox with Selected Text File Contents
txtMain.Text = File.ReadAllText(OpenFileDialog1.FileName)

I then want to read through each line of the file and check the first 3 characters. If the first 3 characters are "LAX" then I know that the next line of the file should start with "CHI"

So I want to read each line, and if it starts with "LAX" I want to check the next line of the file to make sure that the first 3 characters are "CHI" If they are not I want to delete any line breaks / carriage returns from the "LAX" line so it brings the next line to the end of this one. I then want to repeat this task until the first 3 characters of the next line are "CHI"

Basically this ensures that everything relating to LAX is on the same line, as currently this is split over multiple lines in the file.

This is what I've got so far, but I'm stuck with what to do next.

Using streamReader As New StreamReader(OpenFileDialog1.FileName)

    While Not streamReader.EndOfStream
        Dim line As String = streamReader.ReadLine()
        If line.StartsWith("LAX") Then
            ' Go to end of this line and remove CR or LF. Then repeat the process for the next line down until the next line down starts with "CHI"
        End If
    End While
End Using

Does this make sense?

Thank you in advance.

Upvotes: 1

Views: 3005

Answers (2)

Mary
Mary

Reputation: 15091

My Input (contents of Test.txt)

something1
LAX something2
CHI something3
LAX something4
something5
something6
CHI something7
something8

My Output (contents of TextBox1)

something1
LAX something2
CHI something3
LAX something4 something5 something6
CHI something7
something8

The code

Dim line As String = ""
Using streamReader As New StreamReader(OpenFileDialog1.FileName)
     Dim previousLine As String = ""
     While Not streamReader.EndOfStream
         Dim nextLine = streamReader.ReadLine()
         If nextLine.StartsWith("LAX") Or nextLine.StartsWith("CHI") Then
             line &= Environment.NewLine & nextLine
             previousLine = nextLine
         ElseIf previousLine.StartsWith("LAX") Then
             line &= " " & nextLine
         Else
             ine &= Environment.NewLine & nextLine
         End If
     End While
End Using
TextBox1.Text = line

Upvotes: 0

djv
djv

Reputation: 15774

Instead of iterating over the lines like that, find the index of CHI and fix everything before it, and take everything after it. Does that make sense?

' sample text I made up from your description. correct it if it's wrong
Dim text =
$"LAX{Chr(10)}ABC
D{Chr(10)}EFGH{Chr(13)}I
JCHIKLMNOP
QRST
UVW
XY
Z"
' write it to a file
File.WriteAllText("file.txt", text)
' read it back, the rest of the code would be like yours
Dim textFromFile = File.ReadAllText("file.txt")
Dim indexOfChi = textFromFile.IndexOf("CHI")
Dim result = String.Concat(textFromFile.Take(indexOfChi))
' remove line feeds
result = String.Concat(result.Replace(Chr(10), String.Empty))
' remove carriage returns too (these can be done in one line with vbCrLf or Environment.NewLine)
result = String.Concat(result.Replace(Chr(13), String.Empty))
result &= Environment.NewLine & String.Concat(textFromFile.Skip(indexOfChi))
MessageBox.Show(result)

Admittedly this is not the most efficient code but it seems to work.

Upvotes: 0

Related Questions