vb.net - r.peek()>=0 行 = r.readline()

vb.net - r.peek()>=0 line = r.readline()

我正在使用 vb.net 读取文本文件并查看是否有匹配的记录,然后挑选出该记录并将其写入 excel。

然而,通过 peek 循环,它会读取文件直到文件结束。我有 2 个 peek 循环 - 外部 peek 循环查找项目,内部循环搜索以找到它。

peek 循环可以停止,并从外部循环停止的地方继续吗?这是我的代码示例。我在两个搜索循环之外做了一个 for 循环,以在外循环停止的地方继续,但是那样也不对。谢谢

 For i = 1 To t_idex

            rder.BaseStream.Seek(0, IO.SeekOrigin.Begin)
            rder.DiscardBufferedData()

            Do While rder.Peek() >= 0
                line = rder.ReadLine()
                r_idx += 1
                st_outer += 1

                m = Mid(line, 3, 7)
                P = Mid(line, 19, 7)
                b= Trim(Mid(line, 34, 14))

                If Len(b) = 14 Then
                    Do While rder.Peek() >= 0
                        line2 = rder.ReadLine()

                        m2 = Mid(line2, 3, 7)
                        P2 = Mid(line2, 19, 7)
                        b2 = Trim(Mid(line2, 34, 14))

                        If b = b2 Then
                            cnt_dups += 1

                        End If

                        r_idx += 1
                    Loop
                End If
                i = st_outer
            Loop

        Next

看看这对你有什么作用....

Dim Lines() As String = File.ReadAllLines("SomeTextFile.txt")

For LineIndex = 0 To Lines.GetUpperBound(0)
    Dim Match1 As String = Mid(Lines(LineIndex).Trim, 34, 14)
    If Match1.Length = 14 Then
        For Each RemainingLine As String In Lines.Skip(LineIndex + 1)
            Dim Match2 As String = Mid(RemainingLine.Trim, 34, 14)
            If Match1 = Match2 Then
                ' We have a duplicate...
                DoSomething()

                ' Resume outer loop
                ' Comment this line to find all duplicates
                ' Uncomment this line to short-circuit exit upon finding
                '     the first duplicate
                Exit For
            End If
        Next
    End If
Next

参考文献: