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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:43:23  来源:igfitidea点击:

What is the use of formfeed and backspace escape strings in Java?

javaescaping

提问by GrowinMan

Is there any practical usage for \rand \bin Java? Could someone give an example where it's used?

在 Java 中\r\b在 Java 中是否有任何实际用途?有人可以举一个例子吗?

采纳答案by aioobe

I usually use \rtogether with System.out.printwhen 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 所述)。

\bcan be used in progress displays also, for example, on a ICMP Ping, you could display a dot when a ping is sent and a \bwhen it is received to indicate the amount of packet loss.

\b也可以在进度显示中使用,例如,在 ICMP Ping 上,您可以在发送 ping 和\b接收到ping 时显示一个点,以指示数据包丢失量。

回答by Rohan Gupta

Form feed is \fand \ris carriage return. \fis 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

回答by Dillshad Rana

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

换页符是一个分页的 ASCII 控制字符。它强制打印机弹出当前页面并在另一页的顶部继续打印。通常,它也会导致回车。欲了解更多详情,请点击此处