我在C#.NET4控制台应用程序中居中文本时遇到问题.
这是我将文本居中的方法:
private static void centerText(String text)
{
int winWidth = (Console.WindowWidth / 2);
Console.WriteLine(String.Format("{0,"+winWidth+"}", text));
}
Run Code Online (Sandbox Code Playgroud)
但是,我只是输出正常输出的输出.但是,如果我使用此行:
Console.WriteLine(String.Format("{0,"+winWidth+"}", "text"));
Run Code Online (Sandbox Code Playgroud)
"文本"应该居中.
我centerText用这两种方法打电话:
private static void drawStars()
{
centerText("*********************************************");
}
private static void title(string location)
{
drawStars();
centerText("+++ Du er nu her: " + location + "! +++");
drawStars();
}
Run Code Online (Sandbox Code Playgroud)