I was wondering if there is a way to replace all instances of CRLF with '\n'. Is there a way to accomplish that?
-
7Have you tried using `String.Replace("\r\n", "\n")`? – ChaosPandion Feb 22 '12 at 18:43
-
http://stackoverflow.com/questions/841396/what-is-a-quick-way-to-force-crlf-in-c-sharp-net – Tim Schmelter Feb 22 '12 at 18:48
3 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.

- 97,670
- 29
- 122
- 130
If you really want to replace a carriage return/linefeed pair with the explicit string "\n" you need to escape the backslash:
value = value.Replace("\r\n", "\\n");

- 15,314
- 5
- 39
- 57
-
@Bob and ^^^ I already tried both of those techniques. But when I open up the file in notepad (view->show symbol->show end of line character), I still see the CRLF symbols. So I am guessing it didn't work. – SoftwareSavant Feb 22 '12 at 19:58
-
Okay, you're processing a file. Are you using `ReadLine` and `WriteLine` to access the file? Since they handle the line separators, you may be tripping yourself up. – HABO Feb 22 '12 at 20:34
-
1View your documnet in a hex editor and verify it there. Notepad++ might be fudging what it sees based on your EOL setting under "Edit > EOL Conversion" If you choose Unix format it will show LF instead of CRLF. – Bob Feb 22 '12 at 21:02
-
-
My untested theory: You may be looking at a file containing embedded control characters, e.g. using Visual Notepad, and not liking what you see. You write an application that uses `ReadLine` to read the file, and `ReadLine` finds the end-of-line by detecting the control characters you want to remove. Being helpful, it doesn't return those characters to your application. The application attempts to fix up the line and the uses `WriteLine` to output the result. To indicate the end-of-line, `WriteLine` helpfully adds a control character or two to the end of the data you supply. Voila. – HABO Feb 23 '12 at 14:22
I think it looks like I have been attempting to replace in the wrong direction.
String.Replace('\n', '\r')
VS
String.Replace('\r', '\n')
Seem's as though everything is working now.

- 2,971
- 15
- 70
- 141

- 9,467
- 27
- 121
- 195