C# 在控制台C中居中文本#

C# 在控制台C中居中文本#,c#,console,centering,C#,Console,Centering,目前,我正在使用此方法将文本置于控制台的中心 string x = "Whatever I want to print goes here"; Console.SetCursorPosition((Console.WindowWidth - InvalidNo.Length) / 2, Console.CursorTop); Console.WriteLine(x); 但是,我现在需要将如下内容放在中心: players[PlayerNo].Name + " has stolen a piec

目前,我正在使用此方法将文本置于控制台的中心

string x = "Whatever I want to print goes here";
Console.SetCursorPosition((Console.WindowWidth - InvalidNo.Length) / 2, Console.CursorTop);
Console.WriteLine(x);
但是,我现在需要将如下内容放在中心:

players[PlayerNo].Name + " has stolen a piece of cheese from " + players[choice - 1].Name
诸如此类

"Player {0}", playernum + 1
有没有办法用我的方法做到这一点,或者我必须使用不同的方法


提前感谢。

您可以使用您的方法完成此操作,您只需首先创建字符串,以便知道它的长度:

string x = players[PlayerNo].Name + " has stolen a piece of cheese from " + players[choice - 1].Name;
然后您可以只使用其余的代码

对于第二个示例,它使用了
WriteLine
方法的重载来获取格式字符串,因此您可以使用
string.format
方法来创建字符串:

string x = String.Format("Player {0}", playernum + 1);

我在那里没有看到任何方法,只是一个甚至不编译的代码片段。那么你所说的方法从外部看是怎样的呢?@Bart:请改进这个问题,尽可能多地提供信息。如果您是新来的,请先进行现场参观;