【问题标题】:Streamreader - Read Next LineStreamreader - 阅读下一行
【发布时间】:2018-11-09 14:21:23
【问题描述】:

我是 VB.NET 新手,如果这是一个简单的问题,请原谅我。

我正在尝试使用 OpenFileFileDialog Box 将文本文件的内容读入多行文本框。我通过执行以下操作来做到这一点:

' Set Properties of OpenFileDialog
With OpenFileDialog1
    .FileName = ""
    .Title = "Open Text File"
    .InitialDirectory = "c:\"
    .Filter = "Text files|*.txt"
    .ShowDialog()
End With

' Populate Textbox with Selected Text File Contents
txtMain.Text = File.ReadAllText(OpenFileDialog1.FileName)

然后我想通读文件的每一行并检查前 3 个字符。 如果前 3 个字符是“LAX”,那么我知道文件的下一行应该以“CHI”开头

所以我想读取每一行,如果它以“LAX”开头,我想检查文件的下一行以确保前 3 个字符是“CHI” 如果不是,我想从“LAX”行中删除任何换行符/回车符,以便将下一行带到这一行的末尾。 然后我想重复这个任务,直到下一行的前 3 个字符是“CHI”

这基本上可以确保与 LAX 相关的所有内容都在同一行,因为目前这在文件中被拆分为多行。

这就是我目前所做的,但我不知道下一步该做什么。

Using streamReader As New StreamReader(OpenFileDialog1.FileName)

    While Not streamReader.EndOfStream
        Dim line As String = streamReader.ReadLine()
        If line.StartsWith("LAX") Then
            ' Go to end of this line and remove CR or LF. Then repeat the process for the next line down until the next line down starts with "CHI"
        End If
    End While
End Using

这有意义吗?

提前谢谢你。

【问题讨论】:

  • 换个角度看。使用 IO.File.ReadAllLines。你会得到一个很好的数组。然后通过循环所有行将数组组合成一个字符串。完成后,您可以在 LAX 后跟 CHI 时添加新行。
  • 其实你应该先调用File.ReadLines(filePath).ToList()。请注意,这是ReadLines 而不是ReadAllLines。这将为您提供List(Of String),您可以根据需要对其进行操作,包括在适当的情况下删除项目。完成后,在该集合上调用 ToArray 并将结果分配给 TextBoxLines 属性。
  • 谢谢the_lotus。我使用了 ReadAllLines 并循环将其添加到文本框。这工作正常。但是,当我循环遍历时: For Each line As String In array txtMain.Text = txtMain.Text & line & vbNewLine 接下来我想找到“LAX”行,然后检查下一行是否以“CHI”开头。请问我该怎么做?我想到了类似下面的内容,但不确定语法: For Each line As String In array If line.StartsWith("LAX") Then ' Not sure what to put here Else txtMain.Text = txtMain.Text & line & vbNewLine Next
  • 谢谢 jmcilhinney - 我已经按照你的建议完成了,我使用 txtmain.lines = mylist.toarray 在我的文本框中获取所有文件。如何通过查看我所在的行的下一行来进一步操作?
  • 找到LAX 后,只需将布尔变量设置为True,然后让循环检查布尔值并在True 时查找CHI

标签: vb.net loops streamreader


【解决方案1】:

我的输入(Test.txt 的内容)

something1
LAX something2
CHI something3
LAX something4
something5
something6
CHI something7
something8

我的输出(TextBox1 的内容)

something1
LAX something2
CHI something3
LAX something4 something5 something6
CHI something7
something8

代码

Dim line As String = ""
Using streamReader As New StreamReader(OpenFileDialog1.FileName)
     Dim previousLine As String = ""
     While Not streamReader.EndOfStream
         Dim nextLine = streamReader.ReadLine()
         If nextLine.StartsWith("LAX") Or nextLine.StartsWith("CHI") Then
             line &= Environment.NewLine & nextLine
             previousLine = nextLine
         ElseIf previousLine.StartsWith("LAX") Then
             line &= " " & nextLine
         Else
             ine &= Environment.NewLine & nextLine
         End If
     End While
End Using
TextBox1.Text = line

【讨论】:

  • @Odell 欢迎您。请点击我的答案左侧的复选标记(勾号)接受我的答案。
【解决方案2】:

不要像那样遍历这些行,而是找到CHI 的索引并修复它之前的所有内容,然后获取它之后的所有内容。这有意义吗?

' sample text I made up from your description. correct it if it's wrong
Dim text =
$"LAX{Chr(10)}ABC
D{Chr(10)}EFGH{Chr(13)}I
JCHIKLMNOP
QRST
UVW
XY
Z"
' write it to a file
File.WriteAllText("file.txt", text)
' read it back, the rest of the code would be like yours
Dim textFromFile = File.ReadAllText("file.txt")
Dim indexOfChi = textFromFile.IndexOf("CHI")
Dim result = String.Concat(textFromFile.Take(indexOfChi))
' remove line feeds
result = String.Concat(result.Replace(Chr(10), String.Empty))
' remove carriage returns too (these can be done in one line with vbCrLf or Environment.NewLine)
result = String.Concat(result.Replace(Chr(13), String.Empty))
result &= Environment.NewLine & String.Concat(textFromFile.Skip(indexOfChi))
MessageBox.Show(result)

诚然,这不是最有效的代码,但它似乎可以工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多