views:

1745

answers:

6

Hi community,

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.

A: 

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

Konrad Rudolph
+2  A: 

string sample = "abc" + Environment.NewLine + "def";

string replaced = sample.Replace(Environment.NewLine, ",");

Bjorn Reppen
+2  A: 

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

Fredrik Kalseth
+6  A: 

Like this:

string s = "hello\nworld";
s = s.Replace(Environment.NewLine, ",");
Steve
+6  A: 

why not:


string s = "foobar\ngork";
string v = s.Replace(Environment.NewLine,",");
System.Console.WriteLine(v);

mmattax
A: 

Thanks for the quick answers. I was actually doing that, but thought that perhaps there was a better way. Silly me :)

Anyways, I'm also doing a Remove to remove the final comma.

Again, thanks :)

Martín Marconcini