title: How can I format string values using a mask in a generic way?
body: <p>Background: We have a system that receives data from another backend system. We handle the displaying of that data, and we have our own XML templates to control how certain things are displayed (i.e. we have our own column templates to dictate what the column headers are, etc.) One thing we would like to support is the ability to provide a mask for these column templates that would apply to the values coming from the backend. Below is a scenario that I'm having trouble with.</p>
  
  <p>Problem: I can't seem to get a simple string format working. I'd like to format a STRING value of four digits (i.e. "1444") in a time format (i.e. "14:44"). I've tried:</p>
  
  <pre><code>String.Format("{0:00:00}", "1444")
  </code></pre>
  
  <p>Note the importance of the input being a STRING. If I supply an int value, the format will work. The reason I cannot use this is because all the data we receive from the backend is in string format, and we'd like for this to be generic (so casting isn't really an option).</p>
  
  <p>By generic, I mean I'd like to specify a mask in our own XML templates, something like:</p>
  
  <pre><code>&lt;MyColumnTemplate Id="MyColumn" Mask="00:00" /&gt;
  </code></pre>
  
  <p>and to use that mask in a string format call for string values? If the mask fails, we could just simply return the original value (as the String.Format() method already does by default).</p>
  
  <p>Edit: To help clarify, here is a simplified version of what I'd like to be able to do in code:</p>
  
  <pre><code>string inputValue = "1444";
  string maskValueFromXml = "00:00";
  
  string mask = "{0:" + maskValueFromXml + "}";
  string myDesiredEndResult = String.Format(mask, inputValue);
  </code></pre>