繁体   English   中英

逐行阅读文本以进行字符串操作

[英]Reading Text line by line to make string manipulation

因此,这是我已经编写的代码:

        Dim MyFile As String = "Path"
        Dim str_new As String
        Dim str_old As String = File.ReadAllText(MyFile)

        Dim sr As New StreamReader(MyFile)
        Dim strLines As String() = Strings.Split(sr.ReadToEnd, Environment.NewLine)
        Dim Character As Integer = 5 'Line 1 always has 5 characters
        For i = 2 To strLines.Length
            Dim PreviousLine As String = sr.ReadLine(i - 1)
            Dim CurrentLine As String = sr.ReadLine(i)
            If CurrentLine.Contains(TextBox1.Text / 100) Then
                If PreviousLine.Contains("divide") Then
                    Exit For
                End If
            End If
            Character = Character + CurrentLine.Length
        Next
        sr.Close()

        str_new = Replace(str_old, (TextBox1.Text / 100), (TextBox3.Text / 100), Character, 1)

            Dim objWriter3 As New System.IO.StreamWriter(MyFile, False)
            objWriter3.Write(str_new)
            objWriter3.Flush()
            objWriter3.Close()

我试图找出一种方法,将长代码文件分成几行,然后检查每一行中的某些字符串。 如果当前行包含字符串,那么我将在行的上方和/或下方进行其他检查,以确保这是字符串的正确实例。 最后,我只想用另一个字符串替换该字符串实例。

一个例子:文本文件

class
...
0.3
divide   <-- Previous Line
0.3     <-- TextBox1.Text is 30
.5
end
  1. 我希望代码超过0.3的第一个实例
  2. 查找第二个实例
  3. 检查上一行以进行除法
  4. 退出循环
  5. 将第二个实例0.3替换为某个值

我已经研究了一段时间了,任何帮助将不胜感激! 〜马特

修订:代码

        Dim MyFile As String = "Path"
        Dim NewFile As String = "Temporary Path"
        Dim PreviousLine As String = ""
        Dim CurrentLine As String = ""
        Using sr As StreamReader = New StreamReader(MyFile)
            Using sw As StreamWriter = New StreamWriter(NewFile)
                CurrentLine = sr.ReadLine
                Do While (Not CurrentLine Is Nothing)
                    Dim LinetoWrite = CurrentLine

                    If CurrentLine.Contains(TextBox1.Text) Then
                        If PreviousLine.Contains("divide") Then
                            LinetoWrite = Replace(CurrentLine, TextBox1.Text, TextBox3.Text)
                        End If
                    End If

                    sw.WriteLine(LinetoWrite)
                    PreviousLine = CurrentLine
                    CurrentLine = sr.ReadLine
                Loop
            End Using
        End Using

        My.Computer.FileSystem.CopyFile(NewFile, MyFile, True)

您正以错误的方式面对问题。 您必须同时设置读取器和写入器,并生成具有所有修改的新文件。 您的代码包含需要改进的各个部分。 在这个答案中,我只是向您展示如何正确使用StreamReader / StreamWriter 样例代码:

Dim MyFile As String = "input path"
Dim OutputFile As String = "output path"
Dim PreviousLine As String = ""
Dim CurrentLine As String = ""
Using sr As StreamReader = New StreamReader(MyFile)
    Using sw As StreamWriter = New StreamWriter(OutputFile)

        CurrentLine = sr.ReadLine

        Do While (Not CurrentLine Is Nothing)

            Dim lineToWrite = CurrentLine

            'Perform the analysis you wish by involving the current line, the previous one and as many other (previous) lines as you wish; and store the changes in lineToWrite. You should call a function here to perform this analysis

            sw.WriteLine(lineToWrite) 'Writing lineToWrite to the new file

            PreviousLine = CurrentLine 'Future previous line
            CurrentLine = sr.ReadLine 'Reading the line for the next iteration
        Loop
    End Using
End Using

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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