简体   繁体   中英

Reading Text line by line to make string manipulation

So to start this is the code I have already written:

        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()

I am trying to figure out a way to break a long code file into lines then check each line for certain strings. If the current line contains the string then I will do additional check on above and/or below lines to make sure This is the correct instance of the string. Finally I want to replace just that instance of the string with a different string.

An example: text file

class
...
0.3
divide   <-- Previous Line
0.3     <-- TextBox1.Text is 30
.5
end
  1. I want the code to go past the first instance of 0.3
  2. Find the second instance
  3. Check previous line for divide
  4. Exit Loop
  5. Replace second instance of 0.3 to some value

I have been looking into this for a while now and any help would be greatly appreciated! ~Matt

Revised: Code

        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)

You are facing the problem in the wrong way. You have to set both, reader and writer, and generate a new file with all the modifications. Your code has various parts which should be improved; in this answer, I am just showing how to use StreamReader / StreamWriter properly. Sample code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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