How are \r and \n different. I think it has something to do with Unix vs. Windows vs. Mac, but I'm not sure how exactly they're difference, and which to search for/match in regexes.
They're different characters. \r
is carriage return, and \n
is linefeed.
On "old" printers, \r
sent the print head back to the start of the line, and \n
advanced the paper by one line. Both were therefore necessary to start printing on the next line.
Obviously that's somewhat irrelevant now, although depending on the console you may still be able to use \r
to move to the start of the line and overwrite the existing text.
More importantly, Unix tends to use \n
as a line separator; Windows tends to use \r\n
as a line separator and Macs (up to OS 9) used to use \r
as the line separator. (Mac OS X is Unix-y, so uses \n
instead; there may be some compatibility situations where \r
is used instead though.)
For more information, see the Wikipedia newline article.
\r is Carriage Return; \n is New Line (Line Feed) ... depends on the OS as to what each means:
In short \r has ASCII value 13 (CR) and \n has ASCII value 10 (LF). Mac uses CR as line delimiter (at least, it did before, I am not sure for modern macs), *nix uses LF and Windows uses both (CRLF).
In addition to @Jon Skeet's answer:
Traditionally Windows has used \r\n, Unix \n and Mac \r, however newer Macs use \n as they're unix based.
- "\r" => Return
"\n" => Newline or Linefeed (semantics)
Unix based systems use just a "\n" to end a line of text.
- Dos uses "\r\n" to end a line of text.
- Some other machines used just a "\r". (Commodore, Apple II, Mac OS prior to OS X, etc..)