Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a quick way to force CRLF in C# / .NET?

How would you normalize all new-line sequences in a string to one type?

I'm looking to make them all CRLF for the purpose of email (MIME documents). Ideally this would be wrapped in a static method, executing very quickly, and not using regular expressions (since the variances of line breaks, carriage returns, etc. are limited). Perhaps there's even a BCL method I've overlooked?

ASSUMPTION: After giving this a bit more thought, I think it's a safe assumption to say that CR's are either stand-alone or part of the CRLF sequence. That is, if you see CRLF then you know all CR's can be removed. Otherwise it's difficult to tell how many lines should come out of something like "\r\n\n\r".

like image 809
Neil C. Obremski Avatar asked May 08 '09 19:05

Neil C. Obremski


People also ask

What does CRLF stand for?

Description. The term CRLF refers to Carriage Return (ASCII 13, \r ) Line Feed (ASCII 10, \n ). They're used to note the termination of a line, however, dealt with differently in today's popular Operating Systems.

Does Windows use CRLF or LF?

Windows uses CRLF because DOS used CRLF because CP/M used CRLF because history. Mac OS used CR for years until OS X switched to LF. Unix used just a single LF over CRLF and has since the beginning, likely because systems like Multics started using just LF around 1965.

What is difference between LF and CRLF?

CR = Carriage Return ( \r , 0x0D in hexadecimal, 13 in decimal) — moves the cursor to the beginning of the line without advancing to the next line. LF = Line Feed ( \n , 0x0A in hexadecimal, 10 in decimal) — moves the cursor down to the next line without returning to the beginning of the line.

Why did Windows use CRLF?

It comes from the teletype machines (and typewriters) from the days of yore. It used to be that when you were done typing a line, you had to move the typewriter's carriage (which held the paper and slid to the left as you typed) back to the start of the line (CR).


1 Answers

input.Replace("\r\n", "\n").Replace("\r", "\n").Replace("\n", "\r\n") 

This will work if the input contains only one type of line breaks - either CR, or LF, or CR+LF.

like image 130
Daniel Brückner Avatar answered Sep 21 '22 01:09

Daniel Brückner