Tag: centering

在C#console app中居中文本仅使用某些输入

我在C#.NET4控制台应用程序中居中文本时遇到问题。 这是我将文本居中的方法: private static void centerText(String text) { int winWidth = (Console.WindowWidth / 2); Console.WriteLine(String.Format(“{0,”+winWidth+”}”, text)); } 但是,我只是得到输出,因为它会正常输出。 但是,如果我使用此行: Console.WriteLine(String.Format(“{0,”+winWidth+”}”, “text”)); “文本”按照应有的方式居中。 我用这两种方法调用centerText : private static void drawStars() { centerText(“*********************************************”); } private static void title(string location) { drawStars(); centerText(“+++ Du er nu her: ” + location + “! +++”); drawStars(); }