Here you can find the source of zeroPad(String s, int fieldLength)
Parameter | Description |
---|---|
s | the given string |
fieldLength | desired total field length including the string |
public static final String zeroPad(String s, int fieldLength)
//package com.java2s; /*/* w w w . j a v a 2 s .c o m*/ * Copyright 2015, Yahoo! Inc. * Licensed under the terms of the Apache License 2.0. See LICENSE file at the project root for terms. */ public class Main { /** * Prepend the given string with zeros. * @param s the given string * @param fieldLength desired total field length including the string * @return a string front padded with zeros. */ public static final String zeroPad(String s, int fieldLength) { char[] chArr = s.toCharArray(); int sLen = chArr.length; if (sLen < fieldLength) { char[] out = new char[fieldLength]; int zeros = fieldLength - sLen; for (int i = 0; i < zeros; i++) { out[i] = '0'; } for (int i = zeros; i < fieldLength; i++) { out[i] = chArr[i - zeros]; } return String.valueOf(out); } return s; } }