Improving multiple find / replace on HTML string
Posted on 2004-10-26
I'm trying to write some code that will be used for making HTML emails. The problem is that the code seems to be very, very inefficient and I'd like to improve it if possible. The code below reads a HTML file into a string then runs a number of string.replace() functions to get the correct output. This is a very stripped down version of what I have:
Dim sr As System.IO.StreamReader = New System.IO.StreamReader("htmlfile.htm")
Dim s As String = sr.ReadToEnd
s = s.Replace("##Date##", "Date value")
s = s.Replace("##ADDRESS##", "Address value")
Below is an example of the HTML in the file (Sorry, I know this bit is a little obvious)
In my code I'm running s = s.replace() more than 10 times, and given this reads the entire string, modifies it and then writes it again I'm thinking it can't be the best way of doing it. Can anyone think of any way that I can do all of the replacements in one go or improve the code in any way?
Thanks in advance.