With string formatting:
static string BoxLine(int totalWidth, string format, params object[] args) { string s = String.Format(format, args); return "+ " + s.PadRight(totalWidth - 4) + " +"; } static string BoxStartEnd(int totalWidth) { return "+" + new String('-',totalWidth-2) + "+"; }
Name it the same as String.Format , but with a width there:
static void Main(string[] args) { const int BoxWidth = 40; Console.WriteLine( BoxStartEnd(BoxWidth) ); Console.WriteLine( BoxLine(BoxWidth, "Does this work: {0} {1}", 42, 64) ); Console.WriteLine( BoxLine(BoxWidth, " -->Yep<--") ); Console.WriteLine( BoxStartEnd(BoxWidth) ); Console.Read(); }
Output:
+--------------------------------------+ + Does this work: 42 64 + + -->Yep<-- + +--------------------------------------+
source share