Instead of doing this, I want to make use of string.format()
to accomplish the same result:
if (myString.Length < 3) { myString = "00" + 3; }
The %s symbol represents a format specifier for Strings, similar to how %d represents a format specifier for decimal numbers. There are many format specifiers we can use. Here are some common ones: %c - Character.
If you're just formatting a number, you can just provide the proper custom numeric format to make it a 3 digit string directly:
myString = 3.ToString("000");
Or, alternatively, use the standard D format string:
myString = 3.ToString("D3");
string.Format("{0:000}", myString);
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