Question

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?

Was it helpful?

Solution

Why not:

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

OTHER TIPS

Like this:

string s = "hello\nworld";
s = s.Replace(Environment.NewLine, ",");

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

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top