Java String Pad Left leftPadString(String sValue, int iLength, String sPadString)

Here you can find the source of leftPadString(String sValue, int iLength, String sPadString)

Description

Left pad the string with the specific character and maximum length.

License

Open Source License

Parameter

Parameter Description
sValue String to be padded
iLength Length of the string after padded.
sPadString Character to add.

Declaration

public static String leftPadString(String sValue, int iLength,
        String sPadString) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   www.j a v  a2  s  . c om
     * Left pad the string with the specific character and maximum length.
     * @param sValue      String to be padded
     * @param iLength      Length of the string after padded.
     * @param sPadString   Character to add.
     * @return
     */
    public static String leftPadString(String sValue, int iLength,
            String sPadString) {
        String sZeros = "";
        String sResult = "";
        int iRemainLength = 0;

        iRemainLength = iLength - sValue.length();

        //System.out.println("iRemainLength: " + iRemainLength);
        //build '0's
        if (iRemainLength != 0) {
            while (iRemainLength-- > 0) {
                sZeros += sPadString;
            }
        }

        sResult = sZeros + sValue;
        //System.out.println("after add zero: " + sResult);

        return sResult;
    }
}

Related

  1. leftPadding(String in, int count, char pad)
  2. leftPadding(String orgStr, String addStr, int strLength)
  3. leftPadInt(int number, int width)
  4. leftPadMultiline(String input, char padChar, int padWidth)
  5. leftPadString(String str, char pad, int length)
  6. leftPadWithZeros(String input, int expectedSize)
  7. leftPadZeros(int value, int digits, StringBuilder sb)
  8. leftZeroPad(String s)
  9. leftZeroPadding(int number, int howManyChar)