HOWTO · Csharp
How to Add a Newline to a String in C#
Learn three ways to add a line break to a C# string: use ` `, `Environment.NewLine`, or replace a marker in existing text.
On this page
To add a line break in a C# string, include the \n line-feed escape, use Environment.NewLine for the current .NET environment, or replace a specific marker in text that already exists. Choose the newline sequence that matches the text’s destination; a file format or network protocol may require a particular sequence.
Add a Line Break to a C# String With \n
The \n escape represents a line-feed (LF) character. Put it where the next line should begin in a string literal. It represents the character itself, rather than selecting a newline convention based on the operating system. The \r escape represents a carriage-return (CR) character; use it when the data format calls for CR, or for a CR/LF pair when that format specifies both characters.
Run this example:
using System;
class Program
{
static void Main()
{
string message = "First line.\nSecond line.";
Console.WriteLine(message);
}
}
First line.
Second line.
This is useful when you control the string and want an explicit LF. If the string will be read by a system that requires another newline convention, use the sequence that system specifies instead.
Use Environment.NewLine for Platform-Specific Line Breaks
Environment.NewLine supplies the newline sequence for the current .NET environment. Use it when creating text intended for that environment, such as a platform-oriented text file. It does not detect the requirements of an unrelated file format or protocol. When those requirements specify LF or CR/LF, use the required sequence directly even if it differs from the current environment’s convention. See Microsoft’s Environment.NewLine documentation.
using System;
class Program
{
static void Main()
{
string message = "First line." + Environment.NewLine + "Second line.";
Console.WriteLine(message);
}
}
First line.
Second line.
Console.WriteLine also ends its output with a newline. The embedded sequence separates the two parts of message; the final newline comes from the output method.
Add a Line Break After a String Is Created
C# strings are immutable, so changing text produces a new string. String.Replace() returns that new value; assign it back to the variable if you want to keep the change. It replaces every occurrence of the specified marker, so each <break> in the input becomes a line break. Replacing a specific marker is safer than replacing every period, since ordinary sentences may contain punctuation that should stay in place. The existing Microsoft String.Replace() documentation describes the method.
using System;
class Program
{
static void Main()
{
string message = "First line.<break>Second line.";
message = message.Replace("<break>", "\n");
Console.WriteLine(message);
}
}
First line.
Second line.
Use a marker that is reserved for this purpose and cannot appear as ordinary text. If the replacement should follow the current environment, replace it with Environment.NewLine instead of \n. For text that you assemble yourself, putting the desired newline in the original string is usually simpler than editing it afterward.
Together, the three approaches cover the main choices: place LF in a literal, use the current environment’s sequence, or convert a reserved marker in existing text. The destination’s format or protocol determines any stricter newline requirement, so a separate platform-to-format mapping is outside this task’s scope.