Mark Gilbert's Blog

Science and technology, served light and fluffy.

System.String has me in knots

You know it’s time to call it a day when System.String starts giving me difficulties.

Or at least, I SHOULD have known.

A few weeks ago I was happily working on a utility for one of my projects, and started seeing some odd behavior out of a logging routine I was using.  I was trying to pass it a string that had carriage return/line feeds in it, and wanted to dump that string out to the console window as well as to a log file:

Sub Main()
MyMethod(“blah” & vbCrLf & “blah2” & vbCrLf & “blah3” & vbCrLf & “blah4” & vbCrLf)
End Sub

Private Sub MyMethod(ByVal CrazyString As String)
‘ Do some stuff
End Sub

What I was seeing was in some cases the carriage returns were being stripped out of the final string.  I decided to trace through it with the debugger to try to figure out what was killing them.  I set a breakpoint just inside of MyMethod and ran it.  I examined the CrazyString parameter value using the Command Window, and saw that the carriage returns were gone – all of my strings were, ahem, strung together.

After a little more tinkering, I tried doing this in the Command Window:

? CrazyString & vbCrLf & “Test”

That actually allowed ALL of the original carriage returns to appear, not just the one I appended in the window.  I then tried this:

? CrazyString & “Test”

That also worked – all of the carriage returns appeared.  Ok, now let’s move on to the truly silly – let’s append an empty string to CrazyString:

? CrazyString & “”

Sure enough, carriage returns manifest.  Ok, so now that we’ve completely walked off the deep end, it’s time to bring in the sanity reinforcements.  I dragged a colleague over to my desk and showed him the silliness above.  After discussing it for a few minutes the thought occurred to me that perhaps something funny was going on with my machine or my installation of Visual Studio, so we walked over to his desk so he could try to reproduce the problem.  Sure enough, after a couple of minutes he had a console app running on his machine that exhibited the exact same behavior.  Yay!  It’s still broken!  Oh, wait…

He then took my Command Window tests one step further with the following:

? CrazyString.ToString

Surely that won’t make a difference – CrazyString is already defined as a string.  For some reason, however, it does make a difference.  “CrazyString” has no carriage returns, but “CrazyString.ToString” does.

We then tried adding a line of code to the app to write out the value to the console window:


The above line succeeded in writing out the carriage returns, and it didn’t require ‘”.ToString” at the end.  After yet some more tinkering, I found that the phantom carriage returns would be preserved through even through successive function calls, but would only manifest themselves if I appended something to the string, or I invoked .ToString on the parameter.

Now, for the final mind-bender.  We tried this expression in the Immediate Window (while in Debug mode):

? CrazyString = CrazyString.ToString

It returns True.


The apparent moral to the story is just to slap .ToString onto every String variable, just so all outputs (Console.WriteLine(), Immediate Window, Command Window, watches, QuickWatch Window, etc.) show the same thing.

December 1, 2009 - Posted by | Visual Studio/.NET


  1. […] The addition of .ToString() will flush the value out into the debugger (this particular approach seems awfully close to the solution described a previous post of mine: […]

    Pingback by Now you see it. Now you don’t. Subclass date constants and the VS Debugger « Mark Gilbert’s Blog | November 23, 2010

  2. […] remembered an old issue he and I troubleshot, documented at System.String has me in knots.  He modified the original attempt to prepend an empty string: <meta name="description3" […]

    Pingback by Meta Insanity – Strings and Meta Tags « Mark Gilbert’s Blog | October 19, 2011

Sorry, the comment form is closed at this time.

%d bloggers like this: