回车和换行的区别

回车符号和换行符号产生背景:     关于“回车”(carriage return)和“换行”(line feed)这两个概念的来历和区别。
在计算机还没有出现之前,有一种叫做电传打字机(Teletype Model 33)的玩意,每秒钟可以打10个字符。但是它有一个问题,就是打完一行换行的时候,要用去0.2秒,正好可以打两个字符。要是在这0.2秒里面,又有新的字符传过来,那么这个字符将丢失。
     于是,研制人员想了个办法解决这个问题,就是在每行后面加两个表示结束的字符。一个叫做“回车”,告诉打字机把打印头定位在左边界;另一个叫做“换行”,告诉打字机把纸向下移一行。
这就是“换行”和“回车”的来历,从它们的英语名字上也可以看出一二。
      后来,计算机发明了,这两个概念也就被般到了计算机上。那时,存储器很贵,一些科学家认为在每行结尾加两个字符太浪费了,加一个就可以。于是,就出现了分歧。
Unix 系统里,每行结尾只有“<换行>”,即“\n”;Windows系统里面,每行结尾是“ <回车><换 行>”,即“\r\n”;Mac系统里,每行结尾是“<回车>”。一个直接后果是,Unix/Mac系统下的文件在Windows里打 开的话,所有文字会变成一行;而Windows里的文件在Unix/Mac下打开的话,在每行的结尾可能会多出一个^M符号。

c语言编程时(windows系统)
\r 就是return 回到 本行 行首 这就会把这一行以前的输出 覆盖掉
如:
int main() {
cout << "hahaha" << "\r" << "xixi" ;

}
最后只显示 xixi 而 hahaha 背覆盖了
\n 是回车+换行 把光标 先移到 行首 然后换到下一行 也就是 下一行的行首拉
int main() {
cout << "hahaha" << "\n" << "xixi" ;

}

via:http://dadoneo.iteye.com/blog/984725

 

Types of Text Line Breaks

Line breaks in text are generally represented in three ways as either \r\n or as \n or \r. The first type of line break (\r\n) is usually created on a windows computer, the second (\n) on Linux and the third kind of line break (\r) on an Apple computer.

In order to remove line breaks from text we must deal with all three types of line breaks as typically your text could come from any of those sources.

Javascript Code for Line Break Removal

Here’s where the javascript magic happens:

//We define the text variable that needs to be cleansed of line breaks.
Var someText = "Here’s your text.\n It has line breaks that need to be removed.\rUsing Javascript.\r\n"; 

//This javascript code removes all 3 types of line breaks
someText = someText.replace(/(\r\n|\n|\r)/gm,"");

The second piece of code with the javascript replace method removes all three types of line breaks by using this bit of a regular expression: \r\n|\n|\r. This tells it to replace all instance of the \r\n then replace all \n than finally replace all \r. It goes through and removes all types of line breaks from the designated text string.

The "gm" at the end of the regex statement signifies that the replacement should take place over many lines (m) and that it should happen more than once (g).

The "g" in the javascript replace code stands for "greedy" which means the replacement should happen more than once if possible. If for some reason you only wanted the first found occurrence in the string to be replaced then you would not use the "g".

Improving Javascript Code for Line Break Removal

In many real world uses of the javascript code it’s probably better to replace the line breaks with a space instead of just removing them entirely.

If for instance we got a column of text from a pdf and we just removed the line breaks entirely we might wind up with words and sentences that run together and are hard to read like this:

Here is a sentence.This sentence runs into itand unfortunately we aremissing some spaces.

Which of course should read like this instead:

Here is a sentence. This sentence runs into it and unfortunately we are missing some spaces.

In order to do this we will update our example by replacing all the types of line breaks with a single space like so:

//This javascript replaces all 3 types of line breaks with a space
someText = someText.replace(/(\r\n|\n|\r)/gm," ");

Removing Extra Spaces from Lines

Now there may be places in the text where double line spaces appear when we just want a single space. To remove all extra white spaces just use this javascript code after the line break removal code:

//Replace all double white spaces with single spaces
someText = someText.replace(/\s+/g," ");

This javascript regex finds multiple whitespaces and replaces them with a single white space.

via:http://www.textfixer.com/tutorials/javascript-line-breaks.php

原文地址:https://www.cnblogs.com/youxin/p/2688039.html