555

What is difference in a string between \r\n, \r and \n? How is a string affected by each?

I have to replace the occurrences of \r\n and \r with \n, but I cannot get how are they different in a string...

I know that \r is like hitting enter and \n is for a new line.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Haya Raed
  • 5,921
  • 5
  • 16
  • 19
  • This is easy to understand if you imagine a typewriter in front of you: \n - rotates the typewriter one line down \r - returns a carriage, i.e. moves the typewriter roller or printer printhead so that the next character is at the beginning of the line instead of at the end. – Eugene W. Aug 02 '23 at 05:29

4 Answers4

691
  • \r = CR (Carriage Return) → Used as a new line character in Mac OS before X
  • \n = LF (Line Feed) → Used as a new line character in Unix/Mac OS X
  • \r\n = CR + LF → Used as a new line character in Windows
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Tom Bowen
  • 8,214
  • 4
  • 22
  • 42
  • 7
    http://en.wikipedia.org/wiki/Newline – Alasjo Mar 15 '13 at 13:03
  • 6
    Windows is \r\n for a new line. – MrBlue Mar 15 '13 at 13:09
  • 8
    `\r` is for Mac OS 9 and under (also back in the days when it was called *System*). Mac OS X mostly uses `\n` (and is a Unix). – Bruno Mar 15 '13 at 13:51
  • 1
    @Matten Ah sorry, the dangers of quickly typing an answer without much attention. – Tom Bowen Mar 15 '13 at 15:02
  • An edit just got rejected on this but I think it may have been correct. It said: `\n` for unix, `\r` for pre-OSX macs – OGHaza Dec 17 '13 at 09:25
  • 6
    While we're at it: http://geek-and-poke.com/geekandpoke/2014/2/5/babylon – cassi.lup Feb 28 '14 at 20:16
  • 1
    Any idea what the purpose is of using two characters to communicate one thing (in windows)? – user391339 Feb 20 '18 at 05:44
  • 3
    @user391339 You have to think about old printers and typewriters. Carriage return sets your position to the most left column, but the same line. Line feed moves one line below, but staying in the same column. You neeed both to create a new line at be set at the left position. It's already explained on Exasm answer (5 years ago). Of course, in modern computers you don't need that, but Windows's always been doing this backward compatibility thing. – mclopez Apr 04 '18 at 19:22
  • 1
    Does it matter in which order you place r & n? Is there a difference between \r\n & \n\r? – jaques-sam Jul 02 '18 at 07:22
269

All 3 of them represent the end of a line. But...

  • \r (Carriage Return) → moves the cursor to the beginning of the line without advancing to the next line
  • \n (Line Feed) → moves the cursor down to the next line without returning to the beginning of the line — In a *nix environment \n moves to the beginning of the line.
  • \r\n (End Of Line) → a combination of \r and \n
H. Pauwelyn
  • 13,575
  • 26
  • 81
  • 144
Exasm
  • 2,731
  • 1
  • 11
  • 5
  • 2
    is there an actual use of \r\n ? I don't see how a line feed cannot start from its beginning. got an example? – alp May 27 '20 at 18:52
  • 4
    @alp - any serial printer, or serial terminal. Centronix compatible typewriters. This is a very common experience by school students working with Arduino UART. – mckenzm Jul 05 '20 at 00:48
  • 1
    Yes, for the most common one, [see](https://stackoverflow.com/questions/5757290/http-header-line-break-style) HTTP Headers – Yagiz Degirmenci Mar 19 '21 at 21:10
  • 2
    @alp Picture a typewriter. You can move to the next line without resetting the carriage to the start of the line. That's where these control characters originated. – Clonkex Jul 06 '22 at 22:54
32

They are normal symbols as 'a' or 'ю' or any other. Just (invisible) entries in a string. \r moves cursor to the beginning of the line. \n goes one line down.

As for your replacement, you haven't specified what language you're using, so here's the sketch:

someString.replaceAll("\r\n", "\n").replaceAll("\r", "\n")
Samathingamajig
  • 11,839
  • 3
  • 12
  • 34
durilka
  • 1,399
  • 1
  • 10
  • 22
18

A carriage return (\r) makes the cursor jump to the first column (begin of the line) while the newline (\n) jumps to the next line and might also to the beginning of that line. So to be sure to be at the first position within the next line one uses both.

Peter Wippermann
  • 4,125
  • 5
  • 35
  • 48
Matten
  • 17,365
  • 2
  • 42
  • 64
  • 2
    I notice when I am parsing multi-line strings, sometimes they are separated just by \n but other times as \r\n. What determines which is used to separate lines? – Tony Jan 05 '17 at 13:57
  • Generally whatever is coded in a library. In higher languages we would code println(). For Serial terminals and printers we need carriage return and line feed or the next lines indent to where the last line ended. This can however be implied in text files, and an appropriate substitution made on the way to TTY or LP. – mckenzm Jul 05 '20 at 00:46