Is there a way to delete a character that has just

2020-01-30 04:05发布

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.

7条回答
Juvenile、少年°
2楼-- · 2020-01-30 04:09

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!

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-01-30 04:11

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

查看更多
Root(大扎)
4楼-- · 2020-01-30 04:14

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.

查看更多
5楼-- · 2020-01-30 04:15

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.

查看更多
不美不萌又怎样
6楼-- · 2020-01-30 04:19

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 doesnt'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.

查看更多
来,给爷笑一个
7楼-- · 2020-01-30 04:28

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");
查看更多
登录 后发表回答