In C#, ReplaceLineEndings() finds every line break in a string and replaces it. Text from different systems uses different breaks: Windows uses \r\n, Linux and macOS use \n, and very old files use \r. This method handles all of them in one call.
With no argument, it converts every break to Environment.NewLine (the style of the computer the program runs on). With a string argument, it converts every break to that text, such as "\n" or " | ". Like all string methods, it returns a new string.
This is an instance member of System.String. Call it on a string variable, such as text.ReplaceLineEndings(...). Calling it on a null variable throws NullReferenceException. C# strings are immutable: ReplaceLineEndings() never changes the original text. It gives back a new string (or the same one when there is nothing to change), so keep the result in a variable. ReplaceLineEndings is available in .NET 6 and later; it does not exist on .NET Framework.
Syntax
Call it with no argument for the system style, or pass the replacement text.
C#
string mixed = "one\r\ntwo\nthree";
string unix = mixed.ReplaceLineEndings("\n"); // every break is \n
string native = mixed.ReplaceLineEndings(); // every break is Environment.NewLine
string flat = mixed.ReplaceLineEndings(" | "); // "one | two | three"
Call
What it does
text.ReplaceLineEndings()
Use the current system's newline everywhere.
text.ReplaceLineEndings("\n")
Force Linux-style breaks.
text.ReplaceLineEndings("\r\n")
Force Windows-style breaks.
text.ReplaceLineEndings(" ")
Join all lines into one line with spaces.
It recognizes \r\n, \n, \r, and also the rarer Unicode breaks: form feed (\f), next line (U+0085), line separator (U+2028), and paragraph separator (U+2029). A \r\n pair counts as one break, not two. A null replacement throws ArgumentNullException.
Library declarations
C#
public string ReplaceLineEndings(); // .NET 6+
public string ReplaceLineEndings(string replacementText); // .NET 6+
Parameters
Parameter
Description
replacementText
Optional. The text that replaces each line break. If you leave it out, Environment.NewLine is used.
Return Value
Return Value
Description
string
A new string with every line break replaced.
C# Examples
Example 1 — Join Mixed Lines into One Line
A string mixes three kinds of line breaks. Replace each with " | " to build a one-line summary.
C#
using System;
class Program {
static void Main() {
string mixed = "one\r\ntwo\nthree\rfour";
string flat = mixed.ReplaceLineEndings(" | ");
Console.WriteLine(flat);
}
}
Output
one | two | three | four
How It Works
Three styles in one string. There is a Windows break after one, a Linux break after two, and an old Mac break after three.
A pair is one break.\r\n becomes a single " | ", not two, so no empty piece appears between one and two.
Any replacement text. The argument does not have to be a newline; here it is a separator for a log or a status bar.
No loop, no Split. One call does the whole job and returns a new single-line string.
Example 2 — Convert to \n and Count the Lines
Convert Windows breaks to \n and watch the length shrink, then count the lines with Split.
C#
using System;
class Program {
static void Main() {
string text = "a\r\nb\nc";
string unix = text.ReplaceLineEndings("\n");
Console.WriteLine("Before: " + text.Length);
Console.WriteLine("After: " + unix.Length);
Console.WriteLine("Lines: " + unix.Split('\n').Length);
}
}
Output
Before: 6
After: 5
Lines: 3
How It Works
Count the characters. The original is a, \r, \n, b, \n, c: six characters.
One character saved. The Windows pair becomes a single \n, so the new string has five characters. The \n after b is already in the target style.
Now splitting is easy. With only one kind of break left, Split('\n') gives exactly three lines and no stray \r at their ends.
Same result on every computer. Because the replacement text is given, this output does not depend on the operating system.
Example 3 — Use the Current System's Newline
Call the no-argument form and check the result against Environment.NewLine.
C#
using System;
class Program {
static void Main() {
string s = "x\ny\r\nz";
string native = s.ReplaceLineEndings();
Console.WriteLine(native);
string expected = "x" + Environment.NewLine + "y" + Environment.NewLine + "z";
Console.WriteLine(native == expected);
}
}
Output
x
y
z
True
How It Works
No argument. The method uses Environment.NewLine, which is \r\n on Windows and \n on Linux and macOS.
Printing looks the same everywhere. The console shows three lines on any system, even though the hidden characters differ.
Build the expected text. Gluing the letters together with Environment.NewLine creates exactly what the method should produce on this machine.
They match. The comparison prints True. Use this form for text shown to the user or written to local files.
Frequently Asked Questions
It replaces every line break (\r\n, \n, \r, and a few Unicode breaks) with Environment.NewLine, or with the text you pass. It returns a new string.
.NET 6 and later. On .NET Framework, chain Replace calls instead: text.Replace("\r\n", "\n").Replace("\r", "\n").
Call text.ReplaceLineEndings("\n"). It also converts lone \r characters, which a simple Replace("\r\n", "\n") would miss.
As one break. The pair is replaced by a single copy of the replacement text.
Not for the no-argument form, because it uses each system's own newline. Pass an explicit replacement such as "\n" when the result must be identical everywhere.