O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required