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

Is there any practical use for \r and \b in Java? Can someone give an example where it was used?

+6
source share
4 answers

I usually use \r with System.out.print when printing a certain percentage of progress.

Try running this in the 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); } } } 
+5
source

Exit using the form \f , not \r . The first is useful for cleaning the screen in the console, while the second is useful for displaying progress (as indicated in aioobe).

\b can also be used in progress indicators, for example, on ICMP Ping, you can display a point when sending a ping and \b when it will be received to indicate the amount of packet loss.

+10
source

Form feed is an ASCII control character. This causes the printer to retrieve the current page and continue printing at the top of the other. Often this also results in a carriage return. Click here for more information.

+1
source

Form feed \f and \r - carriage return. \f used to print characters after it from a new line starting just below the previous character.

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

Conclusion:

 This is before Now new line OverlapORE 13 
-1
source

Source: https://habr.com/ru/post/897082/


All Articles