This works:
print "\033[F\r" + "New String"
The trick is : \033[F
is an ANSI code (which is supported by most terminals) to go back one line. Then, \r
is to go back at the beginning of the line. You can rewrite it from there. You may need to pad it with white spaces to clear it correctly.
I don't think this is supported in the default console in Windows (cmd
), but it is very likely it will work in the powershell. In Linux (and very likely MacOS), should work without any issue.
UPDATE
After 12 years since I posted this answer, it seems \033[F
is no longer working in my tests. Perhaps at that time I did something else that I can't remember now. The \r
works without issues for resetting the position, for example:
static void progress(int current, int total) {
double pp = Math.round((current / total) * 10000) / 100;
System.out.print "\rProgress: " + current + "/" + total + " (" + pp + "%) ";
}
Which you can call in a loop to display a progress.