蛋疼的回车换行符

\r = 回车 = carriage return = CR = 13

\n = 换行 =  line feed = LF = 10

 

在Windows操作系统中,回车=将光标移动到一行的开始,换行=将光标移动到下一行。

在Linux系统中,换行=将光标移动到下一行的开始。

在Mac中,回车=将光标移动到下一行。

 

在Java的BufferedReader中,readLine方法其实是将\r,\n以及\r\n统统认为是一行的分隔符的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/**
 * Reads a line of text.  A line is considered to be terminated by any one
 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
 * followed immediately by a linefeed.
 *
 * @return     A String containing the contents of the line, not including
 *             any line-termination characters, or null if the end of the
 *             stream has been reached
 *
 * @exception  IOException  If an I/O error occurs
 */
public String readLine() throws IOException {
    return readLine(false);
}

 

而readLine的另一个重载,则允许指定是否忽略LF(回车)符号。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/**
 * Reads a line of text.  A line is considered to be terminated by any one
 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
 * followed immediately by a linefeed.
 *
 * @param      ignoreLF  If true, the next '\n' will be skipped
 *
 * @return     A String containing the contents of the line, not including
 *             any line-termination characters, or null if the end of the
 *             stream has been reached
 *
 * @see        java.io.LineNumberReader#readLine()
 *
 * @exception  IOException  If an I/O error occurs
 */
String readLine(boolean ignoreLF) throws IOException {

 

不同的编辑器对回车换行符的支持也不同。

其中Notepad++是都支持,跟Java默认是一样的。

np

在VIM中,恪守了Linux里的规则,回车被显示成一个^M,换行则是换行。

vim

 

这不是纯蛋疼嘛。。。。。。。。。。。。。。。。。。。统一一下不就得了嘛。

posted @   深夜两点  阅读(2633)  评论(3编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示