63

Is there any way to delete the last character from the console, i.e.

Console.WriteLine("List: apple,pear,");
// Somehow delete the last ',' character from the console.
Console.WriteLine(".");
// Now the console contains "List: apple,pear."

Sure, I could create a string first then print that to the console, but I'm just curious to see if I can delete characters directly from the console.

Contango
  • 76,540
  • 58
  • 260
  • 305
  • 6
    By the way, if you use the `string.Join` method, then you won’t have to worry about that extra comma at the end. `Console.WriteLine("List: " + string.Join(",", fruitArray) + ".");` – Jeffrey L Whitledge Mar 04 '11 at 17:43

8 Answers8

89

"\b" is ASCII backspace. Print it to back up one char.

Console.Write("Abc");
Console.Write("\b");
Console.Write("Def");

outputs "AbDef";

As pointed out by Contango and Sammi, there are times where overwriting with a space is required:

Console.Write("\b \b");
Gama11
  • 31,714
  • 9
  • 78
  • 100
John Arlen
  • 6,539
  • 2
  • 33
  • 42
  • 2
    Thanks, this works perfectly. I appreciate your answer - its one of those problems that difficult to find an answer to as its difficult to find search terms that are not ambiguous. – Contango Mar 04 '11 at 15:53
  • 3
    Just realized that if you do a newline straight after the \b, it doesn't work - you have to add a space to "overwrite" the rogue character. The \b controls the cursor, its more like an arrow key left than a backspace. – Contango Mar 04 '11 at 16:15
  • 2
    Actually '\b' will only work in current line. You are not able to override characters in previous line. – Mr. Ree Jul 10 '15 at 03:57
  • This doesn't work on Windows 11 with Windows terminal, sadly... – SimonC Feb 27 '23 at 22:42
52

Console.Write("\b \b"); is probably what you want. It deletes the last char and moves the caret back.

The \b backspace escape character only moves the caret back. It doesn't remove the last char. So Console.Write("\b"); only moves the caret one back, leaving the last character still visible.

Console.Write("\b \b"); however, first moves the caret back, then writes a whitespace character that overwrites the last char and moves the caret forward again. So we write a second \b to move the caret back again. Now we have done what the backspace button normally does.

Sámal Rasmussen
  • 2,887
  • 35
  • 36
20

This will do the trick if you use Write instead of WriteLine.

Console.Write("List: apple,pear,");
Console.Write("\b");  // backspace character
Console.WriteLine(".");

But you actually have lots of control over the console. You can write to any location you wish. Just use the Console.SetCursorPosition(int, int) method.

Jeffrey L Whitledge
  • 58,241
  • 9
  • 71
  • 99
8

To delete a character on the console use

Console.Write("\x1B[1D"); // Move the cursor one unit to the left
Console.Write("\x1B[1P"); // Delete the character

This will properly delete the character before the cursor and move all following characters back. Using the statement below you will only replace the character before the cursor by a white space and not actually remove it.

Console.Write("\b \b");

My proposed solution should work in some other programming languages as well, since it is using ANSI escape sequences.

nullmn
  • 557
  • 2
  • 7
  • 19
  • 1
    Welcome to Stack Overflow! Voting you up as this is a good alternative answer. I do think that both answers are equivalent, as deleting a character, and overwriting it with a space, are probably the same thing from the users point of view. – Contango Dec 31 '18 at 21:35
  • Doesn't seem to work when debugging at least. The "\b \b" probably works better than 'SysCon.SetCursorPosition(SysCon.CursorLeft - bkspcLen, SysCon.CursorTop);', as 'SetCursorPosition' can't be (stdout) redirected to a file, but "\b \b" still looks unpleasing when redirected to a file. I.E. You might be best avoiding the need altogether. – DennisVM-D2i Apr 20 '22 at 10:55
  • @DennisVM-D2i Not sure what you mean. My answer does not include any call to 'SetCursorPosition'. Also the question asked was about deleting characters on the console and not in a file. – nullmn May 21 '22 at 09:14
  • @nullmn I was referring to the other in-built alternative - 'SetCursorPosition', and referring to a slight extension of what's been asked. Your solution works better than 'SetCursorPosition', but the use-case as a whole does not seem so great *if* you also need to redirect the stdout (Standard Output) to a file - one thing to be aware of. I wonder .. if 'Console.Write("\r");' works any better (?). – DennisVM-D2i May 23 '22 at 08:06
6

if you want to delete only one char you can use:

Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop); and Console.Write() again.

if you want to delete more than one char, like an automation, you can store the current Console.CursorLeft in a variable and use that value in Console.SetCursorPosition(--variablename, Console.CursorTop) in a loop to delete many chars you want!

ForceMagic
  • 6,230
  • 12
  • 66
  • 88
3

The above solutions works great unless you're iterating through a for or foreach loop. In that situation you must use a different approach, like

 Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop);
 Console.WriteLine(" ");

It does, however work well also for a string join.

Examples:

List<int> myList = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

for (int i = 0; i < myList.Count; i++)
{
    Console.Write(myList[i] + ", ");
}

Console.WriteLine("\b\b"); //this will not work.

foreach (int item in myList)
{
    Console.Write(item + ", ");
}

//this will work:
Console.SetCursorPosition(Console.CursorLeft - 2, Console.CursorTop);
Console.WriteLine("  ");

//you can also do this, btw
Console.WriteLine(string.Join(", ", myList) + "\b\b");
aldosa
  • 327
  • 1
  • 2
  • 9
  • 3
    This isn't very safe... If you're on a new line (Console.CursorLeft = 0), this will throw an exception. – Andrew Flanagan May 20 '14 at 15:48
  • I guess specifically, you need something more like Console.SetCursorPosition((Console.CursorLeft - 2 + Console.WindowWidth) % Console.WindowWidth, Console.CursorLeft - 2 <= 0 ? Console.CursorTop - 1 : Console.CursorTop); – Andrew Flanagan May 20 '14 at 15:52
2

If you want to keep on writing into the same line,
overwriting the old line content, not creating a new line,
you can also simply write:

Console.Write("\r"); //CR = 'carriage return' char, moves cursor back to 1st pos in current line, but doesn't add a new one which would do '\n'
Console.Write("{0} Seconds...)", secondsLeft);

So if you want to count down from 10 to 0 then continue if would go like:

for (var i = 10; i > 0; i--)
{
    Console.Write("\r");
    Console.Write("{0} seconds left...{1}", i, i == 1 ? "\n" : "");
    Thread.Sleep(1000);
}
lidqy
  • 1,891
  • 1
  • 9
  • 11
1

You could clear the console and then write the new output.

Tony Abrams
  • 4,505
  • 3
  • 25
  • 32
  • 1
    I think this solution doesn't scale well if the console app becomes more involved; you'd have to keep a buffer of what to print and reprint many times. – Zimano May 07 '19 at 19:13