Java 中换页符和退格转义字符串有什么用?
Java中\r
和\b
有什么实际用途吗?有人可以举个例子吗?
Is there any practical usage for \r
and \b
in Java? Could someone give an example where it's used?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
换页转义是
\f
,而不是\r
。前者对于清除控制台中的屏幕很有用,而后者对于进度显示很有用(如aioobe所述)。\b
也可以用于进度显示,例如,在 ICMP Ping 上,您可以在发送 ping 时显示一个点,在收到 ping 时显示\b
来指示丢包量。Formfeed escape is
\f
, not\r
. The former is useful for clearing the screen in a console, whilst the second is useful for progress displays (as stated by aioobe).\b
can be used in progress displays also, for example, on a ICMP Ping, you could display a dot when a ping is sent and a\b
when it is received to indicate the amount of packet loss.在打印某些进度百分比时,我通常将
\r
与System.out.print
一起使用。尝试在终端中运行此命令:
I usually use
\r
together withSystem.out.print
when printing some progress percentage.Try running this in your terminal:
换页符为
\f
,\r
为回车符。\f
用于打印从前一个字符下方开始的新行之后的字符。输出:
Form feed is
\f
and\r
is carriage return.\f
is used for printing characters after it from new line starting just below previous character.Output:
换页是一种分页 ASCII 控制字符。它强制打印机弹出当前页面并继续在另一页的顶部打印。通常,它也会导致回车。如需了解更多详情,请点击此处
Form feed is a page-breaking ASCII control character. It forces the printer to eject the current page and to continue printing at the top of another. Often, it will also cause a carriage return. For further details click here