Ham*_*ver 1 c# prompt edit console-application
有没有办法可以编辑/重写已经由Console.PrintLine()方法打印的某些行?我必须能够编辑提示中显示的任何行.
这是一个关于我正在尝试运行的代码的示例,可能看起来像:
public static void RewriteLine(LineNr, Text)
{
//Code
}
Console.WriteLine("Text to be rewritten");
Console.Writeline("Just some text");
RewriteLine(1, "New text");
Run Code Online (Sandbox Code Playgroud)
示例根据前一代码的输出显示我想要重写的行:
要重写的文本 //此行(已由Console.WriteLine()方法执行的bin)应替换为:"New text"
只是一些文字
它应该如下所示:
public static void RewriteLine(int lineNumber, String newText)
{
int currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor - lineNumber);
Console.Write(newText); Console.WriteLine(new string(' ', Console.WindowWidth - newText.Length));
Console.SetCursorPosition(0, currentLineCursor);
}
static void Main(string[] args)
{
Console.WriteLine("Text to be rewritten");
Console.WriteLine("Just some text");
RewriteLine(2, "New text");
}
Run Code Online (Sandbox Code Playgroud)
发生的事情是你改变光标位置并写一些东西.您应该添加一些代码来处理长字符串.
| 归档时间: |
|
| 查看次数: |
675 次 |
| 最近记录: |