Java退格转义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3328824/
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-08-13 22:12:44  来源:igfitidea点击:

Java backspace escape

javaescaping

提问by Alfred

I just tested the backspace escape as follows:

我刚刚测试了退格转义如下:

System.out.println("Hello\b");

I expected to get the output: Hell
But it was: "Hello" with a square block

我希望得到输出:地狱
但它是:带有方形块的“你好”

anyone knows how java handle this?

有谁知道java如何处理这个?

采纳答案by Kilian Foth

Java doesn't 'handle' that character at all. All it knows about is a byte stream onto which the OS says it can write bytes, and it will happily write bytes into it, including an ASCII backspace.

Java 根本不“处理”那个字符。它所知道的只是一个字节流,操作系统说它可以在其中写入字节,并且很乐意将字节写入其中,包括 ASCII 退格。

What happenswhen the stream receives that character has nothing whatsoever to do with Java, it is totally terminal-dependent. Some terminals will erase the previous character, some will display the backspace character as some weird glyph, or even as a special "backspace character" glyph. Some may drop the character altogether if they can't interpret it, others (most terminal emulators, in fact) will behave differently depending on how they're configured. But all this has nothing to do with Java, they will behave that way whether they're written to by Java or Perl printor C++ coutor whatever else.

什么情况发生时,流接收字符并没有任何关系用java做的,这是完全与终端相关的。有些终端会擦除前一个字符,有些终端会将退格字符显示为某种奇怪的字形,甚至显示为特殊的“退格字符”字形。如果他们无法解释角色,有些人可能会完全放弃角色,其他人(实际上是大多数终端模拟器)将根据他们的配置方式表现出不同的行为。但这一切都与 Java 无关,无论它们是由 Java、Perl print、C++cout或其他任何东西编写的,它们都会以这种方式运行。

回答by BalusC

It should work perfectly fine in Windows command console. This bug is recognizeable as an Eclipse bug: bug 76936. Also see How to get backspace \b to work in Eclipse's console?

它应该在 Windows 命令控制台中工作得很好。此错误可识别为 Eclipse 错误:错误 76936。另请参阅如何让退格 \b 在 Eclipse 的控制台中工作?

回答by smartarse

you are using System.out.println("...") which is essentially System.out.print("...\n")

您正在使用 System.out.println("...") 本质上是 System.out.print("...\n")

Don't use .println, you are just removing the newline ...

不要使用 .println,你只是删除换行符......

回答by Sahil Chhabra

To get your desired Output i.e. Hellyou need to add a spacecharacter after \b. Because \bwill only move the cursor (virtually) to 1 position backward but it won't delete it. All you can do is replace the character to be deleted by space. Thus try the following line to get Hellas output :

要获得所需的输出,即Hell您需要space\b. 因为\b只会将光标(实际上)向后移动 1 个位置,但不会删除它。您所能做的就是将要删除的字符替换为space。因此尝试以下行Hell作为输出:

System.out.println("Hello\b ");

PS: This Solution works on Windows OS and Ubuntu OS. For other OS it may behave differently as explained by @Kilian Foth above.

PS:此解决方案适用于 Windows 操作系统和 Ubuntu 操作系统。对于其他操作系统,它的行为可能与上面@Kilian Foth 所解释的有所不同。

回答by Haoyu Chen

Try to use terminal to execute your class file.

尝试使用终端来执行您的类文件。

 java classname

When IDE execute \b, it will automatically ignore it. Because there is no corresponding library in IDE to run \b. However, if you run them in terminal, it could be executed. All the terminals have that library.

当IDE执行\b时,它会自动忽略它。因为IDE中没有对应的库来运行\b。但是,如果您在终端中运行它们,则可以执行它。所有的终端都有那个库。

回答by user3375626

You might want to write a function to do it:

您可能想编写一个函数来执行此操作:

def static public String backspace (String str){
    return str.substring(0,str.length-1)
}