Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing carriage return and new-line from the end of a string in c#

People also ask

How do I remove a carriage return from a string?

In the Find box hold down the Alt key and type 0 1 0 for the line feed and Alt 0 1 3 for the carriage return. They can now be replaced with whatever you want.

How do you remove a new line character from a string?

Method 2: Use the strip() Function to Remove a Newline Character From the String in Python. The strip() method in-built function of Python is used to remove all the leading and trailing spaces from a string. Our task can be performed using strip function() in which we check for “\n” as a string in a string.

Which removes the '\ n character at the end of the string?

The canonical way to strip end-of-line (EOL) characters is to use the string rstrip() method removing any trailing \r or \n.


This will trim off any combination of carriage returns and newlines from the end of s:

s = s.TrimEnd(new char[] { '\r', '\n' });

Edit: Or as JP kindly points out, you can spell that more succinctly as:

s = s.TrimEnd('\r', '\n');

This should work ...

var tst = "12345\n\n\r\n\r\r";
var res = tst.TrimEnd( '\r', '\n' );

If you are using multiple platforms you are safer using this method.

value.TrimEnd(System.Environment.NewLine.ToCharArray());

It will account for different newline and carriage-return characters.


String temp = s.Replace("\r\n","").Trim();

s being the original string. (Note capitals)