I have a string format that includes two integer variables, each of which must be formatted to a variable length:
int x = 1234;
int y = 42;
int xFormatDigitCount = 7;
int yFormatDigitCount = 3;
var xStringFormat = new string('0', xFormatDigitCount);
var yStringFormat = new string('0' ,yFormatDigitCount);
So far I have managed to get the desired format using the methods of integer variables .ToString():
var xString = x.ToString(xStringFormat);
var yString = y.ToString(yStringFormat);
return $"{xString}-{yString}";
But this seems like overhead, since line interpolation supports the {var: format} format. Is there a way to get my string using only string interpolation without using x and y ToString()?
source
share