Java String Pad Zero zeroPad(String s, int length)

Here you can find the source of zeroPad(String s, int length)

Description

Pad string with leading zeros.

License

Open Source License

Parameter

Parameter Description
s String to pad.
length Length to pad to.

Return

Input string left-padded with '0' characters to specified length.

Declaration


public static String zeroPad(String s, int length) 

Method Source Code

//package com.java2s;
/*   Please see the license information at the end of this file. */

public class Main {
    /**//from   w  ww .  j  a  v a  2  s.c  om
     * Pad string with leading zeros.
     * 
     * @param s
     *            String to pad.
     * @param length
     *            Length to pad to.
     * 
     * @return Input string left-padded with '0' characters to specified length.
     */

    public static String zeroPad(String s, int length) {
        for (int i = s.length(); i < length; i++) {
            s = "0" + s;
        }

        return s;
    }
}

Related

  1. zeroPad(String s, int fieldLength)
  2. zeropad(String s, int i)
  3. zeropad(String s, int len)
  4. zeropad(String s, int len)
  5. zeroPad(String s, int len)
  6. zeroPad(String string, int digits)
  7. zeroPadAfterTo(String hex, int i)
  8. zeroPadding(String str, int len)
  9. zeroPadding(String value, int n)