Java String Leading Character addLeadingZeros(String s, int nZeros)

Here you can find the source of addLeadingZeros(String s, int nZeros)

Description

Prepends nZeros zeros to the given string and returns the resulting string.

License

Open Source License

Declaration

private static String addLeadingZeros(String s, int nZeros) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2007, 2008 Tran Nam Quang.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://from   www. j a  v  a2  s . co  m
 *    Tran Nam Quang - initial API and implementation
 *******************************************************************************/

public class Main {
    /**
     * Prepends <tt>nZeros</tt> zeros to the given string and returns the
     * resulting string.
     */
    private static String addLeadingZeros(String s, int nZeros) {
        char[] zeros = new char[nZeros];
        for (int i = 0; i < zeros.length; i++)
            zeros[i] = '0';
        return String.valueOf(zeros) + s;
    }
}

Related

  1. addLeadingZero(String aString, int aAmountOfZeros)
  2. addLeadingZeroesTo16BitBinaryString(String binaryValue)
  3. addLeadingZeros(Object number, int places)
  4. addLeadingZeros(String deweyCallNum)
  5. addLeadingZeros(String number, int maxDigits)
  6. addLeadingZeros(String str)
  7. addLeadingZerosToNumber(long number, int length)