Take the 2-minute tour ×

I have a string that has some Environment.Newline in it. I'd like to strip those from the string and instead, replace the Newline with something like a comma.

What would be, in your opinion, the best way to do this using C#.NET 2.0?

Thanks in advance.

share|improve this question

5 Answers 5

up vote 16 down vote accepted

Why not:

string s = "foobar\ngork";
string v = s.Replace(Environment.NewLine,",");
share|improve this answer

Like this:

string s = "hello\nworld";
s = s.Replace(Environment.NewLine, ",");
share|improve this answer

Don't reinvent the wheel - just use myString.Replace(Environment.NewLine, ",")

share|improve this answer
string sample = "abc" + Environment.NewLine + "def";
string replaced = sample.Replace(Environment.NewLine, ",");
share|improve this answer

The best way is the builtin way: Use string.Replace. Why do you need alternatives?

share|improve this answer

Your Answer


