Dylan de St Pern
Dylan de St Pern

Reputation: 469

vb.net streamreader jump out of loop

Ok, so I have a text file, the text file has some string data in it that I have to read, the text file looks like this:

start log

start day = 121010

begin detect

1     121010 05 00 21 50 3 00

2     121010 04 01 21 30 2 00

3     121010 04 01 22 40 1 01

What I am doing is reading the log file with stream reader and I have set a condition for reading each line, if the line has any alphabetic or special characters it skips it and moves onto the next, and if the line is shorter than a length that I specify then it will skip it as well. here is my code to do so:

 If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
        Dim filename As String = OpenFileDialog1.FileName
        Dim line As String
        Dim num As Integer
        Dim disaprovedcharacters As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ.,:!@#$%^&*()_-+=[]''?<>\|"
        Using sr As New StreamReader(filename)
            While Not sr.EndOfStream
                line = sr.ReadLine
                If line.Length > 10 Then
                    If Not line.LastIndexOfAny(disaprovedcharacters.ToCharArray) <> -1 Then
                        Using sr2 As New StreamReader(filename)
                            num = line.Substring(0, 5)
                        End Using
                    End If
                End If
            End While
        End Using
    End If

And that works great for me, the line will now have a value of:

1     121010 05 00 21 50 3 00

the problem is that I want to set the first 5 characters (notice the spaces) in the first line of string (the line after the "start log, start day etc.."). I have done this by using:

num = line.substring(0, 5)

and it shows me the correct number. The only problem is, how do I stop the while loop after it has found the first line in the string. For example, the while loop will run until it reaches the first line of string, and then it stops running. Because all I need is that first number, in this case its "1 ". But the message box will show, "1 " then "2 " and so on, because the while loop is running until the end of the file.

Any help would be appreciated, and if I don't make any sense please let me know, but I have tried to explain it as simple as possible.

Upvotes: 1

Views: 683

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460148

Use Exit While:

While Not sr.EndOfStream
    line = sr.ReadLine
    If line.Length > 10 Then
        If Not line.LastIndexOfAny(disaprovedcharacters.ToCharArray) <> -1 Then
            Using sr2 As New StreamReader(filename)
                num = line.Substring(0, 5)
                Exit While
            End Using
        End If
    End If
End While

MSDN:

Immediately exits the While loop in which it appears. Execution continues with the statement following the End While statement. Exit While can be used only inside a While loop. When used within nested While loops, Exit While transfers control to the loop that is one nested level above the loop where Exit While occurs.

Upvotes: 3

Related Questions