views:

84

answers:

8

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
If using the standard java library, how can I do so? Thank you.
user
Why not use commons-lang? It has a loot of useful extras.
Bozho
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
@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
@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
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
That does not work with Strings (as the OP indicated).
Bart Kiers
but I would like to adding leading zero before a string instead of an int.
user
-1 doesn't work with "apple"
mR_fr0g
+1  A: 

Use Apache Commons StringUtils.leftPad (or look at the code to make your own function).

kaliatech
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
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
+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
+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
That's pretty clever -- but it took me about 30 seconds to "get it". I think a more readable solution would be better.
Coronatus
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
A: 
String input = "Apple";
StringBuffer buf = new StringBuffer(input);

while (buf.length() < 8) {
  buf.insert(0, '0');
}

String output = buf.toString();
RD