How to replace a line break in C# and VB.NET

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

Please Share us:
Please rate this snippet

3 thoughts on “How to replace a line break in C# and VB.NET

Leave a Reply