简体   繁体   中英

vb.net regex comparison to string

im trying to copy the lines until an empty line is detected but this code lags my computer i dont know what im doing wrong it is because im running while loop inside another while loop? here is my code:

ElseIf String.Compare(line, "the") = 1 And Not line.ToLower().Contains("by") Then
            While True

                Dim title = New Regex("^\s*$").Matches(line).Count
                If title = 1 Then Exit While

                builder.AppendLine(line)
                builder.AppendLine(reader.ReadLine())

            End While

You're not resetting the line variable. Something like this should work probably:

While True
    Dim title = New Regex("^\s*$").Matches(line).Count
    If title = 1 Then Exit While
    builder.AppendLine(line)
    line = reader.ReadLine()
End While

EDIT

Instead of regex you could instead just use String.IsNullOrWhiteSpace() which might make your code easier to read in the future.

It's hard to tell exactly what you are trying to do, with that short snippet of code, but the reason why it doesn't work is clear. You will get stuck in an infinite loop if title does not equal 1 . You assume, I think, that the value of line will change reach time you call reader.ReadLine inside the While loop. You probably meant to do something like this:

line = reader.ReadLine()
builder.AppendLine(line)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM