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.
If your string contains numbers only, you can make it an integer and then do padding:
If not I would like to know how it can be done.
You may use apache commons StringUtils
http://commons.apache.org/lang/api-2.3/org/apache/commons/lang/StringUtils.html
Based on @Haroldo Macêdo's answer, I created a method in my custom
Utils
class such asThen call
Utils.padLeft(str, 10, "0");
Use Google Guava:
Maven:
Sample code:
Right padding with fix length-10: String.format("%1$-10s", "abc") Left padding with fix length-10: String.format("%1$10s", "abc")
To format String use
Output : The String : 00000wrwer
Where the first argument is the string to be formatted, Second argument is the length of the desired output length and third argument is the char with which the string is to be padded.
Use the link to download the jar
http://commons.apache.org/proper/commons-lang/download_lang.cgi