Left padding a String with Zeros

jai picture jai · Dec 17, 2010 · Viewed 441.2k times · Source

I've seen similar questions here and here.

But am not getting how to left pad a String with Zero.

input: "129018" output: "0000129018"

The total output length should be TEN.

Answer

khachik picture khachik · Dec 17, 2010

If your string contains numbers only, you can make it an integer and then do padding:

String.format("%010d", Integer.parseInt(mystring));

If not I would like to know how it can be done.