|
-
Jul 9th, 2009, 02:27 AM
#1
Thread Starter
Lively Member
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?
-
Jul 9th, 2009, 02:40 AM
#2
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
-
Jul 9th, 2009, 02:46 AM
#3
Thread Starter
Lively Member
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!
-
Jul 9th, 2009, 03:06 AM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|