How to use \ R and \ n when encoding, the difference between the two _java

Source: Internet
Author: User

What is the difference between \ R and \ n, and how it should be used when coding, let's take a look at it below.

Difference:

\ R:
Full Name: Carriage return (carriage is "word car" meaning, a part on the printer)
Abbreviation: Return
Abbreviation: R
ASCII code: 13
Function: Move the cursor to the left of the current line

\ n:
Full Name: New line
Alias: Line Feed
Abbreviation: n
ASCII code: 10
Action: Move the cursor down one line

How different operating systems represent "carriage return + newline" (that is, the end of a line):
Unix, linux:\n means carriage return + linefeed, \ r doesn't make sense
Windows, dos:\r\n indicate carriage return + line break, order not to change
Mac:\r indicates carriage return + line break

History:
Mechanical English typewriter: On the typewriter, there is a part called "The Word Car" (carriage), each input a word, "word car" forward one. When a full line, the user will push the "word car" to the starting position, when the typewriter will have two actions: "Word car", roller (roller) roll up a line (equivalent to "word cart" Move down one line), so that the next line to start typing. These two actions together are called "carriage returns", which is equivalent to the "Enter" key in the keyboard now.

Telex typewriter: (The TTY concept under the teletype Model 33,linux/unix also comes from this) can be played 10 characters per second. But it has a problem, that is, after a line of line change, to use 0.2 seconds, just can hit two characters. If there are new characters coming in this 0.2 seconds, the character will be lost.
So the developers figured out a way to solve the problem by adding two characters to the end after each line. One is called "return", which tells the typewriter to position the print head at the left edge, and the other is called "line newline", telling the typewriter to move the paper down one line. Later, these two actions were abbreviated as "\ r" and "\ n".

How to use in programming languages:
Because "\ n" is used in UNIX to indicate the end of a line, so in C (and other C language successors, such as C + +, Java) can directly use "\ n", in different operating systems will be automatically converted to the corresponding characters (such as in Windows will be turned into "\ r \ n").

The above is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.