Java String Pad Zero zeropad(String s, int i)

Here you can find the source of zeropad(String s, int i)

Description

zeropad

License

Open Source License

Declaration

public static String zeropad(String s, int i) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    public static String zeropad(String s, int i) {
        return padleft(s, i, '0');
    }/*from  w w w. ja v a2s .  co  m*/

    /**
     * Pad to the left
     *
     * @param s string
     * @param len desired len
     * @param c padding char
     * @return padded string
     */
    public static String padleft(String s, int len, char c) {
        s = s.trim();
        if (s.length() > len) {
            return s;
        }
        final StringBuilder sb = new StringBuilder(len);
        int fill = len - s.length();
        while (fill-- > 0) {
            sb.append(c);
        }
        sb.append(s);
        return sb.toString();
    }
}

Related

  1. zeroPad(final String s, final int fieldLength)
  2. zeroPad(String encodeString, int padding)
  3. zeroPad(String encodeString, int padding)
  4. zeropad(String number, int size)
  5. zeroPad(String s, int fieldLength)
  6. zeropad(String s, int len)
  7. zeropad(String s, int len)
  8. zeroPad(String s, int len)
  9. zeroPad(String s, int length)