public static readonly System.Decimal PadTestValue = 1234.56M;
public static readonly System.String LeftPadTestFormatString = "'{0,15:#,##0.00}'";
public static readonly System.String RightPadTestFormatString = "'{0,-15:#,##0.00}'";
public static readonly System.String LeftPadTestFormatWith = "'{PadTestValue,15:#,##0.00}'";
public static readonly System.String RightPadTestFormatWith = "'{PadTestValue,-15:#,##0.00}'";
public static readonly System.String LeftPadTestFormatOnly = "'{PadTestValue:#,##0.00}'";
public static readonly System.String RightPadTestFormatOnly = "'{PadTestValue:#,##0.00}'";
public static readonly System.String LeftPadTestAlignOnly = "'{PadTestValue,15}'";
public static readonly System.String RightPadTestAlignOnly = "'{PadTestValue,-15}'";
public static readonly System.Text.RegularExpressions.Regex parseRegEx = new System.Text.RegularExpressions.Regex(@"(?<token>([^,:]+))(,(?<alignment>\d+))?(:(?<format>.*))?");
public static readonly System.String ExpectedNumericValueLeftPad = "' 1,234.56'";
public static readonly System.String ExpectedNumericValueRightPad = "'1,234.560 '";
public static readonly System.String ExpectedTextValueLeftPad = "' 1234.56'";
public static readonly System.String ExpectedTextValueRightPad = "'1234.56 '";
public static void Main()
System.Console.WriteLine("System.String.Format(...);");
System.Console.WriteLine("--------------------------");
System.Console.WriteLine(System.String.Format("'{0,15}'","Test"));
System.Console.WriteLine(System.String.Format("'{0,-15}'","Test"));
System.Console.WriteLine(System.String.Format(LeftPadTestFormatString, PadTestValue));
System.Console.WriteLine(System.String.Format(RightPadTestFormatString, PadTestValue));
System.Console.WriteLine();
System.Console.WriteLine("String Interpolation");
System.Console.WriteLine("--------------------");
System.Console.WriteLine($"'{PadTestValue,15}'");
System.Console.WriteLine($"'{PadTestValue,-15}'");
System.Console.WriteLine($"'{PadTestValue,15:#,##0.00}'");
System.Console.WriteLine($"'{PadTestValue,-15:#,##0.00}'");
System.Console.WriteLine();
System.Console.WriteLine("FormatWith Parse Fix");
System.Console.WriteLine("----------------------");
System.Console.WriteLine(Parse("PadTestValue,15:#,##0.00"));
System.Console.WriteLine(Parse("PadTestValue,-15:#,##0.00"));
System.Console.WriteLine(Parse("PadTestValue:#,##0.00"));
System.Console.WriteLine(Parse("PadTestValue:#,##0.00"));
System.Console.WriteLine(Parse("PadTestValue,15"));
System.Console.WriteLine(Parse("PadTestValue,-15"));
System.DateTime startTime = System.DateTime.Now;
for(System.Int32 i = 0; i < 10000; i++)
Parse("PadTestValue,15:#,##0.00");
System.TimeSpan span = System.DateTime.Now - startTime;
System.Console.WriteLine($"Time: {span}");
System.Console.WriteLine();
System.Console.WriteLine("FormatWith Parse RegEx Fix");
System.Console.WriteLine("----------------------");
System.Console.WriteLine(RegexParse("PadTestValue,15:#,##0.00"));
System.Console.WriteLine(RegexParse("PadTestValue,-15:#,##0.00"));
System.Console.WriteLine(RegexParse("PadTestValue:#,##0.00"));
System.Console.WriteLine(RegexParse("PadTestValue:#,##0.00"));
System.Console.WriteLine(RegexParse("PadTestValue,15"));
System.Console.WriteLine(RegexParse("PadTestValue,-15"));
System.DateTime startTime = System.DateTime.Now;
for(System.Int32 i = 0; i < 10000; i++)
RegexParse("PadTestValue,15:#,##0.00");
System.TimeSpan span = System.DateTime.Now - startTime;
System.Console.WriteLine($"Time: {span}");
System.Console.WriteLine();
System.Collections.Generic.Dictionary<System.String, System.Decimal> replacementDictionary = new System.Collections.Generic.Dictionary<System.String, System.Decimal>()
{"PadTestValue", PadTestValue}
private static System.String Parse(System.String tokenKey)
var separatorIdx = tokenKey.IndexOf(":", System.StringComparison.Ordinal);
format = tokenKey.Substring(separatorIdx + 1);
tokenKey = tokenKey.Substring(0, separatorIdx);
var alignmentIdx = tokenKey.LastIndexOf(",", System.StringComparison.Ordinal);
alignment = tokenKey.Substring(alignmentIdx + 1);
tokenKey = tokenKey.Substring(0, alignmentIdx);
string formatString = ((alignmentIdx > -1) ? $",{alignment}" : System.String.Empty) + ((separatorIdx > -1) ? $":{format}" : System.String.Empty);
var fullFormatString = "{0" + formatString + "}";
return(fullFormatString);
private static System.String RegexParse(System.String tokenKey)
System.Text.RegularExpressions.Match results = parseRegEx.Match(tokenKey);
System.String token = results.Groups["token"].Value;
System.String alignment = results.Groups["alignment"].Value;
System.String format = results.Groups["format"].Value;
string formatString = ((!System.String.IsNullOrEmpty(alignment)) ? $",{alignment}" : System.String.Empty) + ((!System.String.IsNullOrEmpty(format)) ? $":{format}" : System.String.Empty);
var fullFormatString = "{0" + formatString + "}";
return(fullFormatString);
throw(new System.Exception("Invalid token"));