Do you mean an auto-generated code definition is for example:
Foo {##} , Bar {Date}
and that will produce:
Foo 01 , Bar 20100420
Foo 02 , Bar 20100420
don't you ?
I think RegExpr.Replace() is a good solution, to the ## problem you can do something like this:
private void Generate()
{
Regex doubleSharpRegEx = new Regex("{#+}");
string customString = "Foo {####}";
string[] generatedCodes = new string[3];
for (int i = 0; i < generatedCodes.Length; i++)
{
string newString = doubleSharpRegEx.Replace(customString,
match =>
{
// Calculate zero padding for format
// remove brackets
string zeroPadding = match.Value.Substring(1, match.Value.Length - 2);
// replace # with zero
zeroPadding = zeroPadding.Replace('#', '0');
return string.Format("{0:" + zeroPadding + "}", i);
});
generatedCodes[i] = newString;
}
}
And the array generatedCodes contains:
Foo 0000
Foo 0001
Foo 0002
Foo 0003
EDIT:
Lambdas expression work only for framework 3.5.
If you need a solution for 2.0, you must only replace the lambda expression part with a delegate (obviously setting i available for the delegated method e.g. class member)
EDIT 2:
You can combine the 2 answer for example in the following code:
private void Generate2()
{
Regex customCodeRegex = new Regex("{CustomCode}");
Regex dateRegex = new Regex("{Date}");
Regex doubleSharpRegex = new Regex("{#+}");
string customString = "Foo-{##}-{Date}-{CustomCode}-{####}";
string newString = customCodeRegex.Replace(customString, "{0}");
newString = dateRegex.Replace(newString, "{1}");
newString = doubleSharpRegex.Replace(newString,
match =>
{
string zeroPadding = match.Value.Substring(1, match.Value.Length - 2);
zeroPadding = zeroPadding.Replace('#', '0');
return "{2:" + zeroPadding + "}";
});
string customCode = "C001";
string date = DateTime.Today.ToString("yyyyMMdd");
string[] generatedCodes = new string[3];
for (int i = 0; i < generatedCodes.Length; i++)
{
generatedCodes[i] = string.Format(newString, customCode, date, i);
}
}