Clearing a line in the console

How can I clear a string on a console in C #?

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

Console.SetCursorPosition(0, Console.CursorTop);
+5
source share
3 answers

The simplest method would be to go to the beginning of the line, as you did, and then write a line of spaces of the same length as the length of the line.

Console.Write(new String(' ', Console.BufferWidth));
+10
source

As soon as the last space of the console buffer line is used, the console cursor automatically moves to the next line.

  • Reset return the cursor to the beginning before it reaches the edge of the console.
  • Delete the old console output by placing the cursor on the next line
  • Reset return the cursor to the line 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;
    }
    
+2
source

( at.toulan Andrew .)

, :

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

If the β€œnew line of text” is shorter than the text that was there before, write spaces before writing the text, as Andrei says.

0
source

All Articles