Environment.NewLine in .NET does not have correct value
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 b开发者_运维知识库etween 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.
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.
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();
}
精彩评论