So I want to create a script that first prints out values from a (pretty big) 2D array and then change one character by erasing it and printing a new one without clearing out the whole console and redrawing it because that creates flickering which is very annoying when i want to make often changes. So is there any way to replace or erase one character that has been already printed or any other efficient way to do it? I just don't want flickering.
Asked
Active
Viewed 1,417 times
1
-
You want to modify the console window for output that has already been displayed, without creating a new line? – Greg Nov 08 '18 at 16:01
2 Answers
1
move the cursor and write a space " ". (Or whatever character you want to use as a replacement.)

magiccheese
- 111
- 2
- 9
0
To delete the last character input in the console you can use the following command:
Console.Write("\b");
However, if you're attempting to remove a character in the middle of the string you'd have to do some clever backspacing to the character you want, and then reprint the rest of the string. You could write a loop to backspace until the character you wish to remove, add the removed characters to a stack, and pop them off to rewrite it.
The second approach would be very inefficient due to console commands being naturally slow, and also really misusing the console backspace command.

Daniel Siebert
- 368
- 3
- 12
-
but i want to erase something that has already been printed and most likely in another line – magiccheese Nov 08 '18 at 16:05
-
You can move the cursor around the Console with `Console.SetCursorPosition(int, int)`. There are other console operations you can do from there. Take a look at: https://stackoverflow.com/questions/22089645/how-to-change-cursor-position-in-console – Flydog57 Nov 08 '18 at 16:08
-
1Unfortunately I believe that the console is not a good I/O for what you're trying to achieve. There are plenty of better objects such as a StringBuilder and outputting the results to a file that would achieve the same thing but with more support for string modifications. – Daniel Siebert Nov 08 '18 at 16:08
-
i do know about `Console.SetCursorPosition(int, int)` but i can't just move my cursor and use `Console.Write("\b \b");`, that doesn't work – magiccheese Nov 08 '18 at 16:10
-
@DanielSiebert outputting files to a file is a bad idea cause i want a live overview what is happening and what is StringBuilder? – magiccheese Nov 08 '18 at 16:15
-
@Dodison Just move the cursor and write a space " ". (Or whatever character you want to use as a replacement.) – Filip Milovanović Nov 08 '18 at 16:16
-
@FilipMilovanović wow that actually helped, i didn't the the answer was so simple. – magiccheese Nov 08 '18 at 16:19