tags:

views:

57

answers:

2

Environnent.NewLine seems to be resolving to two spaces " " (as are vbCrLf and ControlChars.CrLF). Using StringBuilder, AppendLine is doing the same.

I've been racking my brain and searching the Internet trying to figure out why this the way it is, but am coming up empty.

I am trying to generate .bat file based on user interface decisions. I need to have separation between lines. I'm trying:

Dim sb as New StringBuilder
With sb
.Append("Something")
.AppendLine()
.Append("Something else{0}", Environment.NewLine)
.Append("Third line")
End With

When I resolve sb.ToString(), everything is on one line. Where crlf should be, there are two spaces (hex 20).

This is with Visual Studio 2010 Ultimate.

Help! Thanks.

+1  A: 

StringBuilder.Append doesn't take a format string. You want AppendFormat.

Dim sb as New StringBuilder
With sb
.Append("Something")
.AppendLine()
.AppendFormat("Something else{0}", Environment.NewLine)
.Append("Third line")
End With

Also, depending on where you're viewing it (as in, Visual Studio debugger windows) - the newlines may be replaced.

Mark Brackett
I'm not sure this is entirely the case (though definitely an issue with the code in his post)- note that he has an AppendLine in there but everything's still being written to one line.
MisterZimbu
@MisterZimbu - My psychic debugging tells me that he wrote out the BAT file; noticed that he wasn't getting his new lines; tried vbCRLF and ControlChars.CrLF; looked in the debugger window and saw the spaces; posted to SO asking "why are these spaces". If he goes back to the BAT file, he'll see that only his Environment.NewLines aren't showing. The *real* problem is AppendFormat - it's just been obscured by where he left off the debugging exercise and anonymized code.
Mark Brackett
A: 

Are you certain this is the case? Have you tried outputting your string to a file or console to verify that you are not getting newlines?

I'd recommend at least outputting to console window to verify your problem first, then comment on this.

For what it's worth, when I copied your code and outputted to a WinForms richtextbox control, the output was as follows:

Something
Something else
Third line

My source code (C#) is as follows:

    void Form1_Load(object sender, EventArgs e)
    {
        StringBuilder sb = new StringBuilder();
        sb.Append("First Line").AppendLine().Append(String.Format("SecondLine{0}", Environment.NewLine)).Append("ThirdLine");
        richTextBox1.Text = sb.ToString();
    }
Brian Driscoll
This is embarassing. Indeed, everything is working properly. I was getting messed up by copy and pasting from the Immediate window, where the CrLf's were being replaced by spaces.Thanks you for help. Sorry to have wasted everyone' time.
rht341