Search code examples
javastringformattingformat

How to format a Java string with leading zero?


Here is the String, for example:

"Apple"

and I would like to add zero to fill in 8 chars:

"000Apple"

How can I do so?


Solution

  • In case you have to do it without the help of a library:

    ("00000000" + "Apple").substring("Apple".length())
    

    (Works, as long as your String isn't longer than 8 chars.)