Robby Smet
Robby Smet

Reputation: 4661

Remove linebreaks before and after text

I have the following text:

"\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\nSome text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text\r\n \r\nSome other text Some other text Some other text Some other text Some other text Some other text\r\n \r\n\r\n\r\n\r\n".

How can I remove the \r\n before and after the text but keep them in the middle of the text?

Upvotes: 0

Views: 48

Answers (3)

helb
helb

Reputation: 7793

The String.Trim() method removes leading and trailing whitespace (including line breaks of any kind). Assuming that you would want to remove leading and trailing tabs and spaces also, you can simply use:

text = text.Trim();

Upvotes: 3

Sudhakar Tillapudi
Sudhakar Tillapudi

Reputation: 26209

You can use Environment.NewLine as argument to Trim() method to remove all the newline characters from the string.

Try This:

 string newStr = str.Trim(Environment.NewLine.ToCharArray());

EDIT: Environment.NewLine works based on the O.S platform under which you are running the application.

Upvotes: 1

Selman Genç
Selman Genç

Reputation: 101731

You can use String.Trim method:

Removes all leading and trailing occurrences of a set of characters specified in an array from the current String object.

str = str.Trim('\r','\n');

Upvotes: 3

Related Questions