I need to add "123" and zeros for any string - but the resulting string must be exactly 12 characters long.
For example:
28431 = 123000028431
987 = 123000000987
2 = 123000000002
How to do this in C#?
Well, you could use:
string result = "123" + text.PadLeft(9, '0');
In other words, split the task in half - one part generating the "000028431", "000000987" etc part using string.PadLeft
, and the other prefixing the result with "123" using simple string concatenation.
There are no doubt more efficient approaches, but this is what I'd do unless I had a good reason to believe that efficiency was really important for this task.
var result = string.Format("123{0}", number.PadLeft(9, '0'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With