One common task in text processing is to replace line breaks in a string for various reasons. For instance, we might want to create a single, continuous string without any line breaks to save space or remove unwanted formatting. Alternatively, we might need to change all line breaks to another character or sequence of characters, such as a space or a tab, to make the string more readable or compatible with other applications.

To download the source code for this article, you can visit our GitHub repository.

Let’s dive in.

Line Breaks in C#

A character or sequence of characters marks the end of a line of text with a line break. We can use two main types of line breaks in C#:

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!
  • \n: The newline character, used in Unix and Linux systems.
  • \r: The carriage return character, paired with the newline character like so; \r\n used in Windows systems.

The recommended way to write a line break is to use Environment.Newline which translates to \n or \r\n depending on the system.

Next, let’s explore the various options for replacing line breaks in a string in C#. 

Replace Line Breaks the Old Way

The String class’s Replace() method, introduced in .NET Framework 1.1, allows for the replacement of portions of a string. It enables the substitution of all occurrences of a character or substring with another character or substring.

The Replace() method necessitates two arguments for execution: the “old value” string and the “new value” string. Consequently, it can be utilized to modify line breaks within a string. For instance:

const string text = "This is a line.\rThis is another line.";
var newText = text.Replace("\r\n", "\n").Replace("\r", "\n");

In this case, we use the Replace() method to substitute any newline characters with (\n).

This gives us the result:

This is a line.\nThis is another line.

Replace Line Breaks the New Way

Introduced in .NET 6, the ReplaceLineEndings() method is a more specialized tool for replacing line breaks compared to the earlier Replace() method. As such, it’s the recommended approach for line break replacement in C#. This method requires a single argument: the “new string”, which defaults to Environment.NewLine, representing the newline character for the current platform. However, we can specify any string as the replacement string.

For instance, we can use \n to replace all line breaks with the Unix newline character:

const string text = "This is a line.\rThis is another line.";
var newText = text.ReplaceLineEndings("\n");

Here, we use the ReplaceLineEndings() method to substitute any newline characters, specifically carriage return (\r), with newline (\n).

We get the result:

This is a line.\nThis is another line.

The Regular Expression Way

Regular Expressions, introduced as a powerful tool for string manipulation within .NET Framework 1, enable precise pattern matching and character pattern replacement in strings. This makes the Regex.Replace() method a valuable choice for handling line breaks.

This method requires three arguments: the “input string”, the “regex pattern”, and the “replacement string”.

And just as before, we can replace all line breaks with the (\n) newline character using the Replace() method:

const string text = "This is a line.\rThis is another line.";
var newText = Regex.Replace(text, @"(\r\n|\r)", "\n");

Here we use the Regex.Replace() method to replace any newline characters, specifically (\r), with the newline character (\n).

And again, we get the result:

This is a line.\nThis is another line.

Analyze the Performance of the Replacement Methods

Now, let’s delve into the evaluation of three distinct approaches for replacing line breaks within a string that we’ve covered so far. To precisely quantify their execution efficiency, we rely on the BenchmarkDotNet library:

[Benchmark]
public string StringReplace() =>
    ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceMethod();

[Benchmark]
public string StringReplaceLineEndings() =>
    ReplaceLineBreak.ReplaceLineBreaksUsingTheStringReplaceLineEndingsMethod();

[Benchmark]
public string RegexReplace() =>
    ReplaceLineBreak.ReplaceLineBreaksUsingTheRegularExpressionReplaceMethod();

Here we created descriptive functions that contain the code snippets for each method of replacing line breaks. Then we use these functions in benchmark methods that have the [Benchmark] attribute from the BenchmarkDotNet library.

Now we can run the benchmark and assess the results:

| Method                   | Mean      | Error    | StdDev   | Gen0   | Allocated |
|------------------------- |----------:|---------:|---------:|-------:|----------:|
| StringReplace            |  24.69 ns | 0.048 ns | 0.042 ns | 0.0153 |      96 B |
| StringReplaceLineEndings |  79.84 ns | 0.086 ns | 0.077 ns | 0.0153 |      96 B |
| RegexReplace             | 201.44 ns | 0.238 ns | 0.198 ns | 0.0153 |      96 B |

As we can see, the string Replace() method is the fastest among the three methods. The string ReplaceLineEndings() method is about three times slower than the string Replace() method. Conversely, the regular expressions Replace() method is the slowest among the three methods.

In terms of memory allocation, all three methods have the same amount of allocated bytes. This means that none of the methods is more memory-efficient than the others.

Conclusion

So, in this article, we have learned how to replace line breaks in a string in C#. We have discussed three methods: the string Replace() method, the string ReplaceLineEndings() method, and the Regular Expressions Replace() method. The string Replace() method is the fastest and simplest way to substitute line breaks with another substring. The string ReplaceLineEndings() method is more specialized and readable, but also slower than the string Replace() method. It can handle different types of line breaks more easily. The Regular Expressions Replace() method is the most flexible and robust option, but also the slowest. It can use complex patterns to match and replace substrings. These methods are some of the most common ways to replace line breaks in a string in C#, but they are not the only ones. The best method will depend on the specific needs of each situation.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!