Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to replace line breaks in a string in C#?
In C#, you often need to remove or replace line breaks from strings when processing text data. Line breaks can appear as (line feed), \r (carriage return), or \r (Windows-style line endings). This article demonstrates different approaches to handle line breaks in strings.
Common Line Break Characters
Different operating systems use different line break characters −
− Line Feed (LF), used on Unix/Linux/Mac\r− Carriage Return (CR), used on older Mac systems\r− Carriage Return + Line Feed (CRLF), used on Windows
Using String.Replace() Method
The Replace() method is the simplest approach to replace specific line break sequences −
Example
using System;
class Program {
static void Main(string[] args) {
string testString = "Hello \r<br> beautiful <br>\t world";
Console.WriteLine("Original: " + testString.Replace("\r", "\r").Replace("<br>", "\<br>").Replace("\t", "\t"));
string replacedValue = testString.Replace("\r<br>", "_").Replace("<br>", "_").Replace("\r", "_");
Console.WriteLine("Replaced: " + replacedValue);
}
}
The output of the above code is −
Original: Hello \r<br> beautiful <br>\t world Replaced: Hello _ beautiful _ world
Using Regular Expressions
Regular expressions provide a more flexible approach to handle multiple line break patterns at once −
Example
using System;
using System.Text.RegularExpressions;
class Program {
static void Main(string[] args) {
string testString = "Hello \r<br> beautiful <br> world \r end";
Console.WriteLine("Original: " + testString.Replace("\r", "\r").Replace("<br>", "\<br>"));
string replacedValue = Regex.Replace(testString, @"\r<br>|\r|<br>", " ");
Console.WriteLine("Replaced: " + replacedValue);
}
}
The output of the above code is −
Original: Hello \r<br> beautiful <br> world \r end Replaced: Hello beautiful world end
Using Environment.NewLine
For platform-specific line breaks, use Environment.NewLine to handle the current system's line ending −
Example
using System;
class Program {
static void Main(string[] args) {
string multilineText = "Line 1" + Environment.NewLine + "Line 2" + Environment.NewLine + "Line 3";
Console.WriteLine("Original:");
Console.WriteLine(multilineText);
string singleLine = multilineText.Replace(Environment.NewLine, " | ");
Console.WriteLine("\nReplaced:");
Console.WriteLine(singleLine);
}
}
The output of the above code is −
Original: Line 1 Line 2 Line 3 Replaced: Line 1 | Line 2 | Line 3
Comparison of Methods
| Method | Best For | Performance |
|---|---|---|
| String.Replace() | Simple, known patterns | Fast |
| Regex.Replace() | Complex patterns, multiple types | Slower but flexible |
| Environment.NewLine | Platform-specific line breaks | Fast |
Conclusion
Replacing line breaks in C# strings can be accomplished using String.Replace() for simple cases, Regex.Replace() for complex patterns, or Environment.NewLine for platform-specific handling. Choose the method that best fits your specific requirements and performance needs.
