Environment.NewLine
In the VB.NET programming language, you can access the Environment.NewLine
property. This String
equals \r\n. Using NewLine
leads to clear and readable code.
In this language, we can alternatively use the vbCrLf
value directly. This value helps with compatibility with older programs. We review this useful property.
First, we will look at the Environment.NewLine
property. This property simply returns the string
literal equal to vbCrLf
, which is equal to "\r\n" in C# and other languages.
Environment.NewLine
is more descriptive, in that it indicates exactly what it is.Environment.NewLine
and alternative representations.Module Module1 Sub Main() ' Create two-line string. Dim value As String = "[First" + _ Environment.NewLine + _ "Second]" ' Write to console. Console.WriteLine(value) End Sub End Module[First Second]
This is another way you can insert a newline. The vbCrLf
is a special symbol that is compiled into the string
literal "\r\n" as represented in other languages such as the C# language.
string
literal. Its effect is equivalent to the + operator shown in the first example.Module Module1 Sub Main() ' Use vbCrLf. Dim value As String = "[First" & vbCrLf & "Second]" ' Write to console. Console.WriteLine(value) End Sub End Module[First Second]
You are probably curious what the intermediate language of this program looks like. The vbCrLf
is the same as "\r\n" in the C# language.
string
literal is created and used directly in the program.string
is "\r\n".L_0001: ldstr "[First\r\nSecond]"
Depending on your preferences, you can either use Environment.NewLine
or vbCrLf
. For many programmers, Environment.NewLine
is easier to read, so should be preferred.