578

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.

1
  • 5
    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.
    Commented Aug 2, 2023 at 5:29

4 Answers 4

715
  • \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
9
  • 8
    en.wikipedia.org/wiki/Newline
    – Alasjo
    Commented Mar 15, 2013 at 13:03
  • 7
    Windows is \r\n for a new line.
    – MrBlue
    Commented Mar 15, 2013 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
    Commented Mar 15, 2013 at 13:51
  • 7
    While we're at it: geek-and-poke.com/geekandpoke/2014/2/5/babylon
    – cassi.lup
    Commented Feb 28, 2014 at 20:16
  • 4
    @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
    Commented Apr 4, 2018 at 19:22
276

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
4
  • 3
    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
    Commented May 27, 2020 at 18:52
  • 5
    @alp - any serial printer, or serial terminal. Centronix compatible typewriters. This is a very common experience by school students working with Arduino UART.
    – mckenzm
    Commented Jul 5, 2020 at 0:48
  • 1
    Yes, for the most common one, see HTTP Headers Commented Mar 19, 2021 at 21:10
  • 4
    @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
    Commented Jul 6, 2022 at 22:54
33

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")
20

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.

2
  • 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
    Commented Jan 5, 2017 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
    Commented Jul 5, 2020 at 0:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.