7

How can a line in the console be cleared in C#?

I know how to place the cursor at the beginning of a line:

Console.SetCursorPosition(0, Console.CursorTop);
braX
  • 11,506
  • 5
  • 20
  • 33
artoon
  • 729
  • 2
  • 14
  • 41
  • 2
    Google is your friend http://msdn.microsoft.com/en-us/library/system.console.clear.aspx <- first google result, first result for line : http://stackoverflow.com/questions/5027301/c-sharp-clear-console-last-item-and-replace-new-console-animation\ – Benjamin Gruenbaum Mar 14 '13 at 22:36

5 Answers5

14

Simplest method would be to move to the start of the line, as you have done, and then write out a string of spaces the same length as the length of the line.

Console.Write(new String(' ', Console.BufferWidth));
Andrew Cooper
  • 32,176
  • 5
  • 81
  • 116
  • 2
    This seems to wrap to a new line for me. How about Console.Write(new String(' ', Console.BufferWidth - 1)) ?? – Paul Matthews May 13 '14 at 04:09
  • sure it does... it simply writes the complete line! Just move up one line afterwards. If you do -1, you will leave one character unblanked! – Zordid Oct 18 '17 at 09:27
3

Once the last space of a console buffer row is used, the console cursor automatically jumps to the next line.

  1. Reset cursor back to the beginning before it reaches edge of console
  2. Erase old console output, placing cursor on next line
  3. Reset cursor back onto the line that was just cleared

    while (true)
    {
      Console.Write(".");
      if (Console.CursorLeft + 1 >= Console.BufferWidth)
      {
        Console.SetCursorPosition(0, Console.CursorTop);
        Console.Write(Enumerable.Repeat<char>(' ', Console.BufferWidth).ToArray());
        Console.SetCursorPosition(0, Console.CursorTop - 1);
      }
    
      if (Console.KeyAvailable)
        break;
    }
    
aj.toulan
  • 1,341
  • 1
  • 16
  • 25
3

(Combining at.toulan and Andrew's answers here.)

Simplest is, to overwrite over the last line:

Console.SetCursorPosition(0, Console.CursorTop - 1)
Console.WriteLine("new line of text");

If "new line of text" is shorter than the text that was there before, write spaces before writing your text, like Andrew says.

knocte
  • 16,941
  • 11
  • 79
  • 125
1

After setting the cursor position, you can use backspace:

do { Console.Write("\b \b"); } while (Console.CursorLeft > 0);
Thomas Ertl
  • 96
  • 1
  • 4
0

Well i think most of the answers here arent really that robust.

I guess this is solved allready but yk:

private static void ClearPrint(string msg)
{
    Console.WriteLine($"\r{msg}{new String(' ', Console.BufferWidth - msg.Length)}");
}
xVice1337
  • 13
  • 1
  • 1
  • 5
  • Remember that Stack Overflow isn't just intended to solve the immediate problem, but also to help future readers find solutions to similar problems, which requires understanding the underlying code. This is especially important for members of our community who are beginners, and not familiar with the syntax. Given that, **can you [edit] your answer to include an explanation of what you're doing** and why you believe it is the best approach? – Jeremy Caney Jun 25 '23 at 21:05