Results 1 to 4 of 4

Thread: Replacing multiple lines of text in a file

  1. #1

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    69

    Replacing multiple lines of text in a file

    Sorry if this seems like I haven't searched, however I have. Maybe I'm not understanding something or maybe I just haven't found what I need. Anyway my question is this. How would I find multiple strings in a text file and replace them?

    So far I've been able to find out how to replace a single string with:
    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim MyFile() As String = System.IO.File.ReadAllLines(filePath)
            Dim MySW As New System.IO.StreamWriter(filePath, False)
            Using MySW
                For Each Line As String In MyFile
                    MySW.WriteLine(Line.Replace("Example 1", "eExample 1e"))
                Next
            End Using
        End Sub
    End Class
    However I have no idea how to do it for multiple strings. If for example I do something like:
    Code:
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim MyFile() As String = System.IO.File.ReadAllLines(filePath)
            Dim MySW As New System.IO.StreamWriter(filePath, False)
            Using MySW
                For Each Line As String In MyFile
                    MySW.WriteLine(Line.Replace("Example 1", "eExample 1e"))
    MySW.WriteLine(Line.Replace("Example 2", "eExample 2e"))
    MySW.WriteLine(Line.Replace("Example 3", "eExample 3e"))
                Next
            End Using
        End Sub
    End Class
    It replaces everything as per needed however it multiplies the strings by 3 and doesn't replace the multiplied strings anyway. So, how would I go about finding various strings in a text file and replacing them instead of only being able to do it for one string?

  2. #2
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Replacing multiple lines of text in a file

    Well I think you will kick yourself here.

    The problem is that you do the 1st replace and write the line, then you do the 2nd replace on the same input string and write it again, and then again for the third. You need to do each replace on a temporary variable and only write it out when you've done all three.

    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim MyFile() As String = System.IO.File.ReadAllLines(filePath)
            Dim MySW As New System.IO.StreamWriter(filePath, False)
            Dim MyTempString as String = ""
    
            Using MySW
                For Each Line As String In MyFile
                    MyTempString = Line.Replace("Example 1", "eExample 1e")
                    MyTempString = MyTempString.Replace("Example 2", "eExample 2e")
                    MyTempString = MyTempString.Replace("Example 3", "eExample 3e")
                    MySW.WriteLine(MyTempString)
                Next
            End Using
        End Sub
    End Class

  3. #3

    Thread Starter
    Lively Member
    Join Date
    May 2009
    Posts
    69

    Re: Replacing multiple lines of text in a file

    Ah, I figured it was doing something along the lines of fully rewriting the file with replacements then re-writing the file again and again. Just didn't think of using a temp string and variable. Thankyou so much for the help and quick response Paul!

  4. #4
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: Replacing multiple lines of text in a file

    No problem. If thats solved your problem please remember to mark it resolved (using the thread tools menu at the top of the thread).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width