Left padding a String with Zeros?

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 Some answers in my question show who it can be done by replacing spaces by 0 s.

You can do that why if the string doesn't contain spaces.

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.

Some answers in my question show who it can be done by replacing spaces by 0s. You can do that why if the string doesn't contain spaces.

He never said it was a string representing a number... – Joeri Hendrickx Dec 17 '10 at 12:14 @Joeri He posted that example and for that case I mentioned the answers I got for my question and posted a link to my question. – khachik Dec 17 '10 at 12:17.

String str = "129018"; StringBuilder sb = new StringBuilder(); for (int toprepend=10-str. Length; toprepend>0; toprepend--) { sb. Append('0'); } sb.

Append(str); String result = sb.toString().

1 for being the only correct answer that doesn't use an external library – Joeri Hendrickx Dec 17 '10 at 12:15.

Org.apache.commons.lang.StringUtils. LeftPad("129018", 10, "0") the second parameter is the desired output length "0" is the padding char.

String str = "129018"; String str2 = String. Format("%10s", str). Replace(' ', '0'); System.out.

Println(str2).

This is a more round-about approach than the accepted answer. – Paul W May 26 at 12:49.

An old question, but I also have two methods. For a fixed (predefined) length: public static String fill(String text) { if (text.length() >= 10) return text; else return "0000000000". Substring(text.length()) + text; } For a variable length: public static String fill(String text, int size) { StringBuilder builder = new StringBuilder(text); while (builder.length() Append('0'); } return builder.toString(); }.

You may use apache commons StringUtils StringUtils. LeftPad("129018", 10, "0"); commons.apache.org/lang/api-2.3/org/apac....

This is over 2 years old, but it's pretty high up in the results when you Google "java string format pad left," so I think it's useful to improve what's here already. You can replace the "#" characters with whatever character you would like to pad with, repeated the amount of times that you want the total width of the string to be. The benefit of this over khachik's answer is that this does not use Integer.

ParseInt, which can throw an Exception (for example, if the number you want to pad is too large like 12147483647). The disadvantage is that if what you're padding is already an int, then you'll have to convert it to a String and back, which is undesirable. So, if you know for sure that it's an int, khachik's answer works great.

If not, then this is a possible strategy.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions