How do I add line break to a string?

Category: System, viewed: 2K 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
Powered by Disqus