Not that I knew. You can create an extension method if you see that you use it a lot. Assuming you want your line to end in the center, use something like the following
public string PadBoth(string source, int length) { int spaces = length - source.Length; int padLeft = spaces/2 + source.Length; return source.PadLeft(padLeft).PadRight(length); }
To make this an extension method, do it like this:
namespace System { public static class StringExtensions { public static string PadBoth(this string str, int length) { int spaces = length - str.Length; int padLeft = spaces / 2 + str.Length; return str.PadLeft(padLeft).PadRight(length); } } }
Aside, I just include my extensions in the system namespace - it is up to you what you do.
David colwell
source share