有没有办法删除刚刚使用 Console.WriteLine 写入的字符?
有什么方法可以从控制台删除最后一个字符,即
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
“\b”是 ASCII 退格键。打印它来备份一个字符。
输出“AbDef”;
正如 Contango 和 Sammi 所指出的,有时需要用空格覆盖:
"\b" is ASCII backspace. Print it to back up one char.
outputs "AbDef";
As pointed out by Contango and Sammi, there are times where overwriting with a space is required:
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. SoConsole.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.如果您使用
Write
而不是WriteLine
,这就可以解决问题。但实际上你对控制台有很多控制权。您可以写信到您希望的任何位置。只需使用
Console.SetCursorPosition(int, int)
方法即可。This will do the trick if you use
Write
instead ofWriteLine
.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.要在控制台上删除字符,请使用
这将正确删除光标之前的字符并将所有后续字符向后移动。
使用下面的语句只会将光标之前的字符替换为空格,而不会实际删除它。
我提出的解决方案也应该适用于一些其他编程语言,因为它使用 ANSI 转义序列。
To delete a character on the console use
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.
My proposed solution should work in some other programming languages as well, since it is using ANSI escape sequences.
如果您只想删除一个字符,可以使用:
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);
andConsole.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 inConsole.SetCursorPosition(--variablename, Console.CursorTop)
in a loop to delete many chars you want!除非您通过 for 或 foreach 循环进行迭代,否则上述解决方案非常有效。在这种情况下,您必须使用不同的方法,就像
它所做的那样,但是对于字符串连接也很有效。
示例:
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
It does, however work well also for a string join.
Examples:
如果您想继续写入同一行,
覆盖旧行内容,而不是创建新行,
你也可以简单地写:
所以如果你想从 10 倒数到 0 那么继续 if 会像这样:
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:
So if you want to count down from 10 to 0 then continue if would go like:
您可以清除控制台,然后写入新的输出。
You could clear the console and then write the new output.