String.Input and StreamWriter
-
Hi all, I'm working on a method that parses through a html source file, and puts coldfusion comments around script tags. My problem is that the String.Input method is not working with the line read from my streamreader. When using the debugger, it hits the .Input(index, string) line, but it does actually change my string. I think this is because the line is in a stream, but I'm not sure. I know there is a programmatically simple workaround for this, but I haven't done it in so long that I can't remember. Suggestions please - my code is as follows:
private void outputnoscript(StreamReader reader, StreamWriter writer)
{
string line = "";
int opencounter = 0;
int closecounter = 0;while(line!=null) { line = reader.ReadLine(); if (line!=null) { MatchCollection opencollection = Regex.Matches(line, "", RegexOptions.IgnoreCase); for (int i = 0; i < closecollection.Count; i++) { //insert a ---> at the position in the current match, +4 for every ---> already inserted Match closematch = closecollection\[i\]; //plus 8 is to accomodate for the fact that regex returns the position of "/" in "/script>" line.Insert(closematch.Index + 8 + closecounter, "--->"); closecounter += 4; } writer.WriteLine(line); } } }
-
Hi all, I'm working on a method that parses through a html source file, and puts coldfusion comments around script tags. My problem is that the String.Input method is not working with the line read from my streamreader. When using the debugger, it hits the .Input(index, string) line, but it does actually change my string. I think this is because the line is in a stream, but I'm not sure. I know there is a programmatically simple workaround for this, but I haven't done it in so long that I can't remember. Suggestions please - my code is as follows:
private void outputnoscript(StreamReader reader, StreamWriter writer)
{
string line = "";
int opencounter = 0;
int closecounter = 0;while(line!=null) { line = reader.ReadLine(); if (line!=null) { MatchCollection opencollection = Regex.Matches(line, "", RegexOptions.IgnoreCase); for (int i = 0; i < closecollection.Count; i++) { //insert a ---> at the position in the current match, +4 for every ---> already inserted Match closematch = closecollection\[i\]; //plus 8 is to accomodate for the fact that regex returns the position of "/" in "/script>" line.Insert(closematch.Index + 8 + closecounter, "--->"); closecounter += 4; } writer.WriteLine(line); } } }
There is no
Input
member of theString
class, but looking at your code I assume you meanInsert
. Strings are immutable. Performing an operation on a string does not change the string itself, but changes a copy of the string and returns it. So, either use something likestring s = line.Insert(...)
and write that using_writer_.WriteLine
, or use aStringBuilder
which is a mutable string.Microsoft MVP, Visual C# My Articles