仅在包含文本文件中的“the”的控制台行中打印:Visual Basic

时间:2017-09-03 13:23:30

标签: vb.net visual-studio

帮助!! 我已经设定了这个任务,但我真的不确定如何做到这一点:

    Sub Main()

    Dim filePath As String = "C:\...\Projects\testing.txt"
    Dim fileHolder As System.IO.StreamReader
    Dim line As String

    fileHolder = My.Computer.FileSystem.OpenTextFileReader(filePath)
    line = fileHolder.ReadLine()

    While line <> Nothing
        Console.WriteLine(line)
        Console.WriteLine("*****")
        line = fileHolder.ReadLine()
    End While
    Console.ReadKey()

    End Sub

“编辑上面的程序,以便它只将以”The“开头的行写入控制台。您可能会发现重新访问字符串处理课程以完成此任务很有帮助。”

我尝试在while循环中使用if语句或在While语句中使用Do Until循环,但是这意味着它将打印包含“the”的第一行而不是包含“the”的第三行,如第二行没有。 这是迄今为止我失败的(非常糟糕的代码):

    Dim filePath As String = "C:\...\Projects\testing.txt"
    Dim fileHolder As System.IO.StreamReader
    Dim line As String

    fileHolder = My.Computer.FileSystem.OpenTextFileReader(filePath)
    line = fileHolder.ReadLine()
    Dim the As String = "the"

    While line <> Nothing
        If line.ToUpper.Contains(the.ToUpper) Then
            Console.WriteLine(line)
            Console.WriteLine("*****")
            line = fileHolder.ReadLine()
        End If
    End While
    Console.ReadKey()

感谢您的帮助!!

1 个答案:

答案 0 :(得分:1)

试试这个:

Dim filePath As String = "C:\...\Projects\testing.txt"
Dim fileHolder As System.IO.StreamReader
Dim line As String

fileHolder = My.Computer.FileSystem.OpenTextFileReader(filePath)
line = fileHolder.ReadLine()
Dim the As String = "the"

While line <> Nothing
    If line.ToUpper.Contains(the.ToUpper) Then
        Console.WriteLine(line)
        Console.WriteLine("*****")
    End If

    line = fileHolder.ReadLine()
End While
Console.ReadKey()