Add zero-padding to a string

001 picture 001 · Jun 26, 2010 · Viewed 171.8k times · Source

How do I add "0" padding to a string so that my string length is always 4?

Example

If input "1", 3 padding is added = 0001
If input "25", 2 padding is added = 0025
If input "301", 1 padding is added = 0301
If input "4501", 0 padding is added = 4501

Answer

kemiller2002 picture kemiller2002 · Jun 26, 2010

You can use PadLeft

var newString = Your_String.PadLeft(4, '0');