magiccheese
magiccheese

Reputation: 151

Erase only one already printed character from the console in c#

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.

Upvotes: 1

Views: 1931

Answers (2)

magiccheese
magiccheese

Reputation: 151

move the cursor and write a space " ". (Or whatever character you want to use as a replacement.)

Upvotes: 1

Daniel Siebert
Daniel Siebert

Reputation: 378

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.

Upvotes: 0

Related Questions