有没有办法删除刚刚使用 Console.WriteLine 写入的字符?

发布于 2024-10-20 08:03:07 字数 281 浏览 11 评论 0原文

有什么方法可以从控制台删除最后一个字符,即

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

当然,我可以先创建一个字符串,然后将其打印到控制台,但我只是好奇是否可以直接从控制台删除字符。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

吐个泡泡 2024-10-27 08:03:07

“\b”是 ASCII 退格键。打印它来备份一个字符。

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

输出“AbDef”;

正如 Contango 和 Sammi 所指出的,有时需要用空格覆盖:

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

"\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");
沦落红尘 2024-10-27 08:03:07

Console.Write("\b \b"); 可能就是您想要的。它删除最后一个字符并将插入符号移回原位。

\b 退格转义字符仅将插入符号向后移动。它不会删除最后一个字符。因此 Console.Write("\b"); 仅将插入符号向后移动,最后一个字符仍然可见。

Console.Write("\b \b"); 但是,首先将插入符号向后移动,然后写入一个空白字符,覆盖最后一个字符并再次向前移动插入符号。因此,我们编写第二个 \b 来将插入符号再次移回原位。现在我们已经完成了退格按钮通常所做的事情。

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.

诺曦 2024-10-27 08:03:07

如果您使用 Write 而不是 WriteLine,这就可以解决问题。

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

但实际上你对控制台有很多控制权。您可以写信到您希望的任何位置。只需使用 Console.SetCursorPosition(int, int) 方法即可。

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.

夏末 2024-10-27 08:03:07

要在控制台上删除字符,请使用

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

这将正确删除光标之前的字符并将所有后续字符向后移动。
使用下面的语句只会将光标之前的字符替换为空格,而不会实际删除它。

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

我提出的解决方案也应该适用于一些其他编程语言,因为它使用 ANSI 转义序列。

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.

顾北清歌寒 2024-10-27 08:03:07

如果您只想删除一个字符,可以使用:

Console.SetCursorPosition(Console.CursorLeft - 1, Console.CursorTop); 并再次使用Console.Write()

如果您想删除多个字符(例如自动化操作),可以将当前的 Console.CursorLeft 存储在变量中,并在 Console.SetCursorPosition(--variablename, Console. CursorTop) 在循环中删除许多你想要的字符!

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!

热风软妹 2024-10-27 08:03:07

除非您通过 for 或 foreach 循环进行迭代,否则上述解决方案非常有效。在这种情况下,您必须使用不同的方法,就像

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

它所做的那样,但是对于字符串连接也很有效。

示例:

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

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");
怪我入戏太深 2024-10-27 08:03:07

如果您想继续写入同一行,
覆盖旧行内容,而不是创建新行,
你也可以简单地写:

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

所以如果你想从 10 倒数到 0 那么继续 if 会像这样:

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

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);
}
蓬勃野心 2024-10-27 08:03:07

您可以清除控制台,然后写入新的输出。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文