VB newline escape char?

本秂侑毒 提交于 2019-12-23 11:48:12

问题


In C I use "1st line 1\n2nd line" for a newline, but what about VB? I know "1st line" & VbCrLf & "2nd line" but its too verbose, what is the escape char for a newline in VB?

I want to print

1st line
2nd line

I tried using \n but it always outputs this no matter how many times I run the compiler

1st line\n2nd line

Any ideas?


回答1:


You should use Environment.NewLine. That evaluates to CR+LF on Windows, and LF on Unix systems.

There are no escape sequences for CR or LF characters in VB. And that's why "\n" is treated literally. So, Environment.NewLine is your guy.




回答2:


As others have said, you should be using the environment-sensitive Environment.NewLine property. vbCrLf is an old VB6 constant which is provided in VB.NET for backwards compatability. So for instance, you should be using:

"1st line" & Environment.NewLine & "2nd line"

If that's too wordy when you have many lines, you can use the StringBuilder class, instead:

Dim builder As New StringBuilder()
builder.AppendLine("line 1")
builder.AppendLine("line 2")
builder.AppendLine("line 3")
builder.AppendLine("line 4")

If you specifically need the CR and LF characters, you should be using the constants in the ControlChars class instead of the old vbCrLf constant.




回答3:


You say VB, but have tagged with VB.NET. Your question is slightly odd as well. Different OSes actually require different newline characters. What you want is Environment.NewLine. This will give you the correct new line character(s) no matter what OS the .NET framework is running on.




回答4:


I still using the old fashioned chr(13) for example:

"Hello" & chr(13) & "World"

It results in:

Hello
World

EDIT: Like Dave Doknjas said, in your case it would be chr(10) to feed the line.



来源:https://stackoverflow.com/questions/14076179/vb-newline-escape-char

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!