Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Replace CRLF with '\n'

Tags:

string

c#

newline

I was wondering if there is a way to replace all instances of CRLF with '\n'. Is there a way to accomplish that?

like image 775
SoftwareSavant Avatar asked Feb 22 '12 18:02

SoftwareSavant


1 Answers

What have you tried that is not working? CRLF means carriage return, line feed. Carriage return is \r, line feed is \n so replacing CRLF with line feed would be

value = value.Replace("\r\n", "\n");

This is just like any other string replace. It is important to note that Replace is a method which returns a new string, so you need to assign the result back to some variable, in most cases it will probably be the same variable name you were already using.

Edit: based on your comment below, you are probably mistaking CRLF for LF based on Notepad++'s end of line conversion setting. Open your file in a hex editor to see what is really there. You will see CRLF as 0D 0A (so carriage return is 0D and line feed is 0A). Notepad++ will show you what you want to see. Checked your end of line conversion by clicking Edit > EOL Conversion > UNIX Format and it will show LF instead of CRLF, even if a CRLF is there.

like image 196
Bob Avatar answered Oct 04 '22 17:10

Bob