Here you can find the source of zeroPadString(String string, int length)
Parameter | Description |
---|---|
string | the original String to pad. |
length | the desired length of the new padded String. |
public static final String zeroPadString(String string, int length)
//package com.java2s; public class Main { private static final char[] zeroArray = "0000000000000000".toCharArray(); /**//from www . jav a 2 s .c om * Pads the supplied String with 0's to the specified length and returns the * result as a new String. For example, if the initial String is "9999" and * the desired length is 8, the result would be "00009999". This type of * padding is useful for creating numerical values that need to be stored * and sorted as character data. Note: the current implementation of this * method allows for a maximum <tt>length</tt> of 16. * * @param string * the original String to pad. * @param length * the desired length of the new padded String. * @return a new String padded with the required number of 0's. */ public static final String zeroPadString(String string, int length) { StringBuffer buf = new StringBuffer(length); buf.append(zeroArray, 0, length - string.length()).append(string); return buf.toString(); } }