Padding a string with leading zeros

Started the new job here and I was going through the code with the boss of the developers and found a couple of interesting things that I said could be sorted out with a lot short notation. One of the things that I spotted was his looping through something and adding zero’s to the start of it.

I mentioned that it could be sorted our with a single line of code… he was amazed that you could do that.

So here is the code:

response.write Right(String(4,"0") & n, 4)

This is of course done in Classic ASP. All it does is create a string of length 4 and then append the number (n) at the end… easy.

 

Leave a Reply

Your email address will not be published.