Java 中换页符和退格转义字符串有什么用?

发布于 2024-12-03 18:27:48 字数 62 浏览 0 评论 0原文

Java中\r\b有什么实际用途吗?有人可以举个例子吗?

Is there any practical usage for \r and \b in Java? Could someone give an example where it's used?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

后eg是否自 2024-12-10 18:27:48

换页转义是 \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.

成熟稳重的好男人 2024-12-10 18:27:48

在打印某些进度百分比时,我通常将 \rSystem.out.print 一起使用。

尝试在终端中运行此命令:

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);
        }
    }
}

I usually use \r together with System.out.print when printing some progress percentage.

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);
        }
    }
}
你没皮卡萌 2024-12-10 18:27:48

换页符为\f\r 为回车符。
\f 用于打印从前一个字符下方开始的新行之后的字符。

System.out.println("This is before\fNow new line");
System.out.println("TEXTBEFORE\rOverlap");
System.out.println("12\b3");

输出:

This is before
          Now new line
OverlapORE
13

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.

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
痴情换悲伤 2024-12-10 18:27:48

换页是一种分页 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文