【发布时间】:2014-05-14 21:30:54
【问题描述】:
所以我正在尝试编写一个库,以便在控制台中进行一些简单的格式化,这样我就不必每次制作项目时都这样做。包括在其中,我想制作一个将您输入的字符串放入框中的方法。
这是我的代码:
public static void DrawBox(string message, char borderChar, ConsoleColor messageColor, ConsoleColor borderColor, int padTop, int padBottom)
{
for (int i = 0; i < Console.WindowWidth; i++)
{
Console.ForegroundColor = borderColor;
Console.Write(borderChar);
}
for (int i = 0; i < padTop; i++)
{
Console.Write(string.Format("{0,0}" + "{0," + (Console.WindowWidth - 1) + "}",borderChar));
}
Console.ForegroundColor = borderColor;
Console.Write(string.Format("{0,0}", borderChar));
Console.ForegroundColor = messageColor;
Console.Write("{0," + ((Console.WindowWidth / 2) + message.Length / 2) + "}", message);
Console.ForegroundColor = borderColor;
Console.Write("{0," + (((Console.WindowWidth / 5) + message.Length / 5)) + "}", borderChar);
for (int i = 0; i < padBottom; i++)
{
Console.WriteLine(string.Format("{0,0}" + "{0," + (Console.WindowWidth - 1) + "}", borderChar));
}
for (int i = 0; i < Console.WindowWidth; i++)
{
Console.ForegroundColor = borderColor;
Console.Write(borderChar);
}
}
这是可行的,但如果你输入一个更大的字符串就会出错。 我怎么做才能使字符串像这样格式化 不管信息有多大。
* hi *
* world *
【问题讨论】:
-
我已经尝试了各种各样的东西,但仍然没有解决它。帮助将不胜感激
标签: c# string console format alignment