读取文本文件中的某一行并显示下一行 [英] Read certain line in text file and display the next

查看:46
本文介绍了读取文本文件中的某一行并显示下一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 VB.net 的新手,我需要帮助.我想要做的是在文本文件中找到某一行并在文本框中显示下一行.对于第一部分(找到线),我做得很好,但我正在努力处理第二部分,该部分显示的是我找到的线之后的线!

I am new to VB.net and I need help. What I am trying to do is to find a certain line in a text file and display the next line in a textbox. With the first part (finding the line) I'm doing just fine but I am struggling with the second part which is displaying the line that is after the one I have found!

这是我正在使用的测试文件的内容:

Here is the content of the test file that I am using:

哈哈

这就是我到目前为止所做的:

And this is what I have done so far:

    Using sReader As New StreamReader("filepath")

        While Not sReader.EndOfStream
            Dim line As String = sReader.ReadLine()
            If line.Contains("123") Then
                TextBox1.Text = line
            End If
        End While

    End Using

收到的结果:

推荐答案

While Not sReader.EndOfStream
    Dim line As String = sReader.ReadLine()
    If line.Contains("123") AndAlso Not sReader.EndOfStream Then
        Dim nextLine As String = sReader.ReadLine()
        TextBox1.Text = nextLine
        Exit While
    End If
End While

使用 LINQ 可以使完整代码更具可读性:

With LINQ the complete code can be made much more readable:

Dim nextLineAfterMatch = File.ReadLines("filepath").
    SkipWhile(Function(line) Not line.Contains("123")).
    Skip(1).
    FirstOrDefault()

If nextLineAfterMatch IsNot Nothing Then TextBox1.Text = nextLineAfterMatch 

这篇关于读取文本文件中的某一行并显示下一行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆