Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What would be the fastest way to remove Newlines from a String in C#?

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?

like image 760
Martin Marconcini Avatar asked Aug 24 '08 21:08

Martin Marconcini


People also ask

Does trim get rid of newlines?

trim() . The trim method removes any leading or trailing whitespace from a string, including spaces, tabs and all line breaks.

Does string trim remove newlines C#?

To trim a string like that we use C#'s Trim() method. One version of this method accepts characters to cut from both sides of the string. When we specify the carriage return character ( \r ) and line feed character ( \n ) there, the method removes all leading and trailing newlines.

How do I remove a newline from the end of a string?

Use String. trim() method to get rid of whitespaces (spaces, new lines etc.) from the beginning and end of the string.

Does Rstrip remove newline?

The rstrip() method removes any trailing character at the end of the string. By using this method, we can remove newlines in the provided string value.


1 Answers

Why not:

string s = "foobar\ngork";
string v = s.Replace(Environment.NewLine,",");
System.Console.WriteLine(v);
like image 177
mmattax Avatar answered Sep 21 '22 17:09

mmattax