title: Pad left or right with string.format (not padleft or padright) with arbitrary string
body: <p>Can I use String.Format() to pad a certain string with arbitrary characters?</p>
  
  <pre><code>Console.WriteLine("-&gt;{0,18}&lt;-", "hello");
  Console.WriteLine("-&gt;{0,-18}&lt;-", "hello");
  
  returns 
  
  -&gt;             hello&lt;-
  -&gt;hello             &lt;-
  </code></pre>
  
  <p>I now want the spaces to be an arbitrary character.
  The reason I cannot do it with padLeft or padRight is because I want to be able to construct the format string at a different place/time then the formatting is actually executed.</p>
  
  <p><strong>--EDIT--</strong><br>
  Seen that there doesn't seem to be an existing solution to my problem I came up with this (after <a href="https://stackoverflow.com/questions/541098/pad-left-or-right-with-string-format-not-padleft-or-padright-with-arbitrary-str/541210#541210">Think Before Coding's suggestion</a>)<br>
  <strong>--EDIT2--</strong><br>
  I needed some more complex scenarios so I went for <a href="https://stackoverflow.com/questions/541098/pad-left-or-right-with-string-format-not-padleft-or-padright-with-arbitrary-str/541323#541323">Think Before Coding's second suggestion</a></p>
  
  <pre><code>[TestMethod]
  public void PaddedStringShouldPadLeft() {
      string result = string.Format(new PaddedStringFormatInfo(), "-&gt;{0:20:x} {1}&lt;-", "Hello", "World");
      string expected = "-&gt;xxxxxxxxxxxxxxxHello World&lt;-";
      Assert.AreEqual(result, expected);
  }
  [TestMethod]
  public void PaddedStringShouldPadRight()
  {
      string result = string.Format(new PaddedStringFormatInfo(), "-&gt;{0} {1:-20:x}&lt;-", "Hello", "World");
      string expected = "-&gt;Hello Worldxxxxxxxxxxxxxxx&lt;-";
      Assert.AreEqual(result, expected);
  }
  [TestMethod]
  public void ShouldPadLeftThenRight()
  {
      string result = string.Format(new PaddedStringFormatInfo(), "-&gt;{0:10:L} {1:-10:R}&lt;-", "Hello", "World");
      string expected = "-&gt;LLLLLHello WorldRRRRR&lt;-";
      Assert.AreEqual(result, expected);
  }
  [TestMethod]
  public void ShouldFormatRegular()
  {
      string result = string.Format(new PaddedStringFormatInfo(), "-&gt;{0} {1:-10}&lt;-", "Hello", "World");
      string expected = string.Format("-&gt;{0} {1,-10}&lt;-", "Hello", "World");
      Assert.AreEqual(expected, result);
  }
  </code></pre>
  
  <p>Because the code was a bit too much to put in a post, I moved it to github as a gist:<br>
  <a href="http://gist.github.com/533905#file_padded_string_format_info" rel="noreferrer">http://gist.github.com/533905#file_padded_string_format_info</a></p>
  
  <p>There people can easily branch it and whatever :)</p>