Java String Pad Left leftPad(StringBuilder pStringBuilder, int pLength, char pChar)

Here you can find the source of leftPad(StringBuilder pStringBuilder, int pLength, char pChar)

Description

Similar to the Oracle lpad command.

License

Open Source License

Parameter

Parameter Description
pStringBuilder The StringBuffer object to pad.
pLength The length to pad to.
pChar The character to pad with.

Return

StringBuilder the chomped string.

Declaration

public static StringBuilder leftPad(StringBuilder pStringBuilder, int pLength, char pChar) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*from w ww  .  ja va2 s  .  c o  m*/
     * Similar to the Oracle lpad command. Left pads a string.
     *
     * @param pStringBuilder The StringBuffer object to pad.
     * @param pLength The length to pad to.
     * @param pChar The character to pad with.
     * @return StringBuilder the chomped string.
     */
    public static StringBuilder leftPad(StringBuilder pStringBuilder, int pLength, char pChar) {
        StringBuilder lTempBuffer = new StringBuilder(pLength);
        while ((lTempBuffer.length() + pStringBuilder.length()) < pLength) {
            lTempBuffer.append(pChar);
        }
        lTempBuffer.append(pStringBuilder);

        return lTempBuffer;
    }

    /**
     * Similar to the Oracle lpad command. Left pads a string.
     *
     * @param pStr The String object to pad.
     * @param pLength The length to pad to.
     * @param pChar The character to pad with.
     * @return String the padded string.
     */
    public static String leftPad(String pStr, int pLength, char pChar) {
        return leftPad(new StringBuilder(pStr), pLength, pChar).toString();
    }

    /**
     * Similar to the Oracle lpad command. Left pads a string.
     *
     * @param pStr The String object to pad.
     * @param pLength The length to pad to.
     * @return String the padded string.
     */
    public static String leftPad(String pStr, int pLength) {
        return leftPad(new StringBuilder(pStr), pLength, ' ').toString();
    }
}

Related

  1. leftPad(String text, int length, char padChar)
  2. leftPad(String text, int size)
  3. leftPad(String toPad, int numPads)
  4. leftPad(String value, int makeLength, char paddingCharacter)
  5. leftPad(String value, int size, String pad)
  6. leftPadByteArray(byte[] source, int size)
  7. leftPadded(String src, int len)
  8. leftPaddedString(String string, int length, char paddingChar)
  9. leftPadding(final StringBuffer strBuf, final int bufLen, final char fill)