English 中文(简体)
Is there a way to delete a character that has just been written using Console.WriteLine?
原标题:

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.

最佳回答

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

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

outputs "AbDef";

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

Console.Write(" ");
问题回答

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

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

Console.Write(" "); 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  to move the caret back again. Now we have done what the backspace button normally does.

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

Console.Write("List: apple,pear,");
Console.Write("");  // 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.

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(" ");

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

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!

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(""); //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) + "");

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("
"); //CR =  carriage return  char, moves cursor back to 1st pos in current line, but doesn t add a new one which would do  
 
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("
");
    Console.Write("{0} seconds left...{1}", i, i == 1 ? "
" : "");
    Thread.Sleep(1000);
}

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





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签