How to pad left a number with a specific amount of zeroes

C# has the built-in PadLeft() function for this:

someString = someString.PadLeft(8, '0');

And here’s an article on it at Microsoft Learn.

To use a regular expression, do something like this:

string someText = "asd 123 rete"; 
someText = Regex.Replace(someText, @"\d+", n => n.Value.PadLeft(8, '0'));

Leave a Comment