java Java中formfeed和backspace转义字符串有什么用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7378425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the use of formfeed and backspace escape strings in Java?
提问by GrowinMan
Is there any practical usage for \r
and \b
in Java? Could someone give an example where it's used?
在 Java 中\r
和\b
在 Java 中是否有任何实际用途?有人可以举一个例子吗?
采纳答案by aioobe
I usually use \r
together with System.out.print
when printing some progress percentage.
我通常在打印一些进度百分比时\r
一起使用System.out.print
。
Try running this in your terminal:
尝试在您的终端中运行它:
class Test {
public static void main(String[] args) throws InterruptedException {
for (int i = 0; i < 100; i++) {
System.out.print("Progress: " + i + " %\r");
Thread.sleep(100);
}
}
}
回答by squidge
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).
换页转义是\f
,不是\r
。前者对于清除控制台中的屏幕很有用,而第二个对于进度显示很有用(如 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.
\b
也可以在进度显示中使用,例如,在 ICMP Ping 上,您可以在发送 ping 和\b
接收到ping 时显示一个点,以指示数据包丢失量。
回答by Rohan Gupta
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.
换页是\f
和\r
是回车。
\f
用于从前一个字符下方的新行开始打印其后的字符。
System.out.println("This is before\fNow new line");
System.out.println("TEXTBEFORE\rOverlap");
System.out.println("12\b3");
Output:
输出:
This is before
Now new line
OverlapORE
13