Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java backspace escape

Tags:

java

escaping

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?

like image 847
Alfred Avatar asked Jul 25 '10 10:07

Alfred


People also ask

What is backslash F in Java?

Java code for the escape sequence \f: // This \f escape sequence is a form feed character. // It is an old technique and used to indicate a page break. public class Test { public static void main(String[] args) {

What does \t do in Java?

What does \t mean in Java? This means to insert a new tab at this specific point in the text. In the below example, "\t" is used inside the println statement. It is similar to pressing the tab on our keyboard.


1 Answers

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.

What happens when 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 print or C++ cout or whatever else.

like image 131
Kilian Foth Avatar answered Sep 29 '22 22:09

Kilian Foth