How do I add line break to a string?

Category: System, viewed: 401 time(s).

To add line break to a string you can use the Environment.NewLine constant or you can directly use the line break string literal such as \r\n or just \n.

using System;

namespace Kodecsharp.Example.System
{
    class LineBreak
    {
        public static void Main(string[] args)
        {
            //
            // We use a string literal, the "\r\n" constant to
            // create a line breaks
            //
            string text = "First Line\r\nSecond Line" + "\r\n" +
                "Third Line";
            Console.WriteLine(text);

            //
            // We can also use the Environment.NewLine to get the new
            // line constant of the current platform. The line break
            // is different for instance between Windows and Unix (if
            // you are using Mono).
            //
            string anotherText = "First Level" + Environment.NewLine +
                "Second Level" + Environment.NewLine +
                "Third Level";
            Console.WriteLine(anotherText);

            Console.ReadLine();
        }
    }
}

The program above create the following output:

First Line
Second Line
Third Line
First Level
Second Level
Third Level
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Our Friends

Statistics

Locations of visitors to this page