Here is the String, for example: "Apple", and I would like to add zero to fill in 8 chars. I would like to show the result like this;' "000Apple" How can I do so? Thank you.
+4
A:
StringUtils.leftPad(yourString, 8, '0');
This is from commons-lang. See javadoc
Bozho
2010-10-29 12:37:41
If using the standard java library, how can I do so? Thank you.
user
2010-10-29 12:43:59
Why not use commons-lang? It has a loot of useful extras.
Bozho
2010-10-29 12:45:56
Even if you are not able to use commons-lang, you can easily copy the source from StringUtils to make your own function. That would be a much better general solution than the selected answer. http://www.docjar.com/html/api/org/apache/commons/lang/StringUtils.java.html
kaliatech
2010-10-29 13:01:48
@Bozho, what if that would be the only method the library is used for? Perhaps the added library is even many times bigger than the app it is used in. I can imagine quite some reasons *not* to add a commons library in an application. Don't get me wrong: I agree, it contains very useful stuff, but I understand the reluctance to stuff an app full of external JARs if the benefit is not needing to write just one (or a couple) of methods.
Bart Kiers
2010-10-29 13:05:22
@kaliatech: yes, a much better GENERAL solution, but if he don't want to use the library probably a focused (short) solution is more appropriate.
Arne
2010-10-29 13:08:24
A:
public class LeadingZerosExample {
public static void main(String[] args) {
int number = 1500;
//
// String format below will add leading zeros (the %0 syntax) to the number above.
// The length of the formatted string will be 7 characters.
//
String formatted = String.format("%07d", number);
System.out.println("Number with leading zeros: " + formatted);
}
}
infinity
2010-10-29 12:37:45
+1
A:
Use Apache Commons StringUtils.leftPad (or look at the code to make your own function).
kaliatech
2010-10-29 12:38:51
A:
It isn't pretty, but it works. If you have access apache commons i would suggest that use that
if (val.length() < 8) {
for (int i = 0; i < val - 8; i++) {
val = "0" + val;
}
}
mR_fr0g
2010-10-29 12:43:06
The -1 is probably because this is a not so good example: you're using "magic numbers" and are concatenating Strings, something that should be replaced by using a StringBuilder or StringBuffer.
Bart Kiers
2010-10-29 12:58:37
+1
A:
public class PaddingLeft {
public static void main(String[] args) {
String input = "Apple";
String result = "00000000" + input;
int length = result.length();
result = result.substring(length - 8, length);
System.out.println(result);
}
}
Arne
2010-10-29 12:45:14
+5
A:
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.)
Chris Lercher
2010-10-29 12:48:21
That's pretty clever -- but it took me about 30 seconds to "get it". I think a more readable solution would be better.
Coronatus
2010-10-29 13:12:29
A:
You may have to take care of edgecase. This is a generic method.
public class Test {
public static void main(String[] args){
System.out.println(padCharacter('0',8,"hello"));
}
public static String padCharacter(char c, int num, String str){
for(int i=0;i<=num-str.length()+1;i++){str = c+str;}
return str;
}
}
Bragboy
2010-10-29 12:50:17
A:
String input = "Apple";
StringBuffer buf = new StringBuffer(input);
while (buf.length() < 8) {
buf.insert(0, '0');
}
String output = buf.toString();
RD
2010-10-29 12:53:21