Replacing a line break is a simple challenge. In .NET there are plenty of ways to do that.
You can for example use String.Replace or Regex.Replace. My prefered method is String.Replace but both versions will do it.

String.Replace Method for c#

sampleString = sampleString.Replace(System.Environment.NewLine, "replacement string");

String.Replace Method for vb.net

sampleString = sampleString.Replace(System.Environment.NewLine, "replacement string")

Regex.Replace Method for c#

sampleString = Regex.Replace(sampleString, @"\r\n?|\n", "replacement string");

Regex.Replace Method for c#

sampleString = Regex.Replace(sampleString, "\r\n?|\n", "replacement string")

more informations can be found in the msdn Regex.Replace, String.Replace

3 thought on “How to replace a line break in C# and VB.NET”
  1. I suggest:
    sampleString = sampleString.Replace(sampleString, “r”, “”).Replace(sampleString, “n”, “replacement string”)

Leave a Reply