Replacing newline characters in VB.NET strings
I’m not very familiar (which I am actually very happy for!) with VB.NET and so I had a hard time figuring out how to trim newline characters from a string in a Reporting Services report I was working on.
Turns out none of the “regular” methods really work. The Trim function only removes spaces and anything like Replace(“string”, “\n”, “”) didn’t work.
You have to use the Chr(13) or Chr(10) functions to actually get the character values for the newline characters. So, my solution looked like this:
Replace(Replace(stringVariable, Chr(10), “”), Chr(13), “”)
Thanks to this forum post for providing the clues: http://www.daniweb.com/forums/thread54797.html


The “newline character” you mentioned doesn’t work because the spaces you see are basically a carriage return and a linefeed character: “\r\n”. This is specific for the operating system and implementation of the .NET Framework. Technically the carriage return actually denotes that the cursor should move to the beginning of the current line and the linefeed moves to the next one. Replacing just the “\n” will obviously leave the carriage return and therefore still format as a new line. In this case, you must remove both the “\r\n” which you’ve obviously found, but this can be simplified. This can be done in a few ways.
First of all you could use the vbCrLf constant like so:
Replace(stringVariable, vbCrLf, “”)
This is exactly Chr(13) + Chr(10) and will also create a variable in memory to use for the empty string. Thus, another way might be more suitable.
Second you could use Environment.NewLine and String.Empty like so:
Replace(stringVariable, Environment.NewLine, String.Empty)
This will grab the environment specific newline sequence which in this case is “\r\n” and use the existing empty string constant to replace the matches.
I guess the environment specific one to look for would be up to the operating system the value was entered in though, essentially.