Insert a New Line in a String

Problem

You need to insert a line break or tab character in a string, typically for display purposes.

Solution

Use the NewLine property of the System.Environment class, or use the global vbTab and vbNewLine constants.

Discussion

Visual Basic .NET provides three equivalent approaches for inserting line breaks. It’s most common to use the System.Environment class, which provides a NewLine property that returns the new line character for the current platform:

Dim MyText As String
MyText = "This is the first line."
MyText &= Environment.NewLine
MyText &= "This is the second line."

However, the System.Environment class doesn’t provide a property for tabs. Instead, you can use the traditional Visual Basic-named constants vbNewLine ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.