Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

Replace Line Breaks in a String C#

How can I replace Line Breaks within a string in C#?
by

1 Answer

akshay1995
The solutions posted so far either only replace Environment.NewLine or they fail if the replacement string contains line breaks because they call string.Replace multiple times.

Here's a solution that uses a regular expression to make all three replacements in just one pass over the string. This means that the replacement string can safely contain line breaks.

string result = Regex.Replace(input, @"\r\n?|\n", replacementString);

Login / Signup to Answer the Question.