C# String ReplaceLineEndings() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
String

Definition

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"
CallWhat 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

ParameterDescription
replacementTextOptional. The text that replaces each line break. If you leave it out, Environment.NewLine is used.

Return Value

Return ValueDescription
stringA 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);
    }
}

How It Works

  1. Three styles in one string. There is a Windows break after one, a Linux break after two, and an old Mac break after three.
  2. A pair is one break. \r\n becomes a single " | ", not two, so no empty piece appears between one and two.
  3. Any replacement text. The argument does not have to be a newline; here it is a separator for a log or a status bar.
  4. 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);
    }
}

How It Works

  1. Count the characters. The original is a, \r, \n, b, \n, c: six characters.
  2. 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.
  3. Now splitting is easy. With only one kind of break left, Split('\n') gives exactly three lines and no stray \r at their ends.
  4. 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);
    }
}

How It Works

  1. No argument. The method uses Environment.NewLine, which is \r\n on Windows and \n on Linux and macOS.
  2. Printing looks the same everywhere. The console shows three lines on any system, even though the hidden characters differ.
  3. Build the expected text. Gluing the letters together with Environment.NewLine creates exactly what the method should produce on this machine.
  4. 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.

Next: Split()

Break a string into an array of pieces at a separator.

Split() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

14 people found this page helpful