This question already has an answer here:
- How can I pad a String in Java? 27 answers
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.
Here's another approach:
Just asked this in an interview........
My answer below but this (mentioned above) is much nicer->
String.format("%05d", num);
My answer is:
Here is a solution based on String.format that will work for strings and is suitable for variable length.
Output: '000000test' 'test' 'test' '0test'
Check my code that will work for integer and String.
Assume our first number is 129018. And we want to add zeros to that so the the length of final string will be 10. For that you can use following code
Here's my solution:
Output: 00000101