Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. String.Input and StreamWriter

String.Input and StreamWriter

Scheduled Pinned Locked Moved C#
regexhtmldatabasedebuggingtools
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    j1e1g1
    wrote on last edited by
    #1

    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);
    			}
    		}
    	}
    
    H 1 Reply Last reply
    0
    • J j1e1g1

      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);
      			}
      		}
      	}
      
      H Offline
      H Offline
      Heath Stewart
      wrote on last edited by
      #2

      There is no Input member of the String class, but looking at your code I assume you mean Insert. 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 like string s = line.Insert(...) and write that using _writer_.WriteLine, or use a StringBuilder which is a mutable string.

      Microsoft MVP, Visual C# My Articles

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups