17

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?

Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
Alfred
  • 1,709
  • 8
  • 23
  • 38

6 Answers6

29

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.

Aria Pahlavan
  • 1,338
  • 2
  • 11
  • 22
Kilian Foth
  • 13,904
  • 5
  • 39
  • 57
10

To get your desired Output i.e. Hell you need to add a space character after \b. Because \b will 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 Hell as output :

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.

Sahil Chhabra
  • 10,621
  • 4
  • 63
  • 62
  • 1
    @StackOverflowException - The reason for your output has been already explained by Kilian Foth. Your OS must be different from Windows and Ubuntu. – Sahil Chhabra Aug 09 '13 at 15:01
3

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?

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks, but it still did not work in windows console as well. did you try this out on your machine. maybe it is my system problem. – Alfred Jul 25 '10 at 21:31
  • I've used \r in Windows Console (carriage return without line feed) and it works fine. I use it to show a "processing NN%" chaging text. – helios Jun 08 '11 at 11:39
-3

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.

Haoyu Chen
  • 1,760
  • 1
  • 22
  • 32
-3

You might want to write a function to do it:

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

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

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

  • 1
    In that case println("Hello\b") would emit "Hello\b\n" not "Hello\n\b". And that doesn't explain the glyph. – helios Jun 08 '11 at 11:40