Java String Pad Left leftPadding(String in, int count, char pad)

Here you can find the source of leftPadding(String in, int count, char pad)

Description

Applies a left padding to the provided string.

License

Open Source License

Parameter

Parameter Description
in String to left pad
count Minimum string length to achieve
pad Character to pad with

Return

String

Declaration

static String leftPadding(String in, int count, char pad) 

Method Source Code

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

public class Main {
    /**/*from  www.ja  v  a 2  s.c o m*/
     * Applies a left padding to the provided string. If the string is already count in length,
     * no change will be made. Otherwise, the pad character will be prepended until count length
     * is satisfied.
     *
     * @param in    String to left pad
     * @param count Minimum string length to achieve
     * @param pad   Character to pad with
     * @return String
     */
    static String leftPadding(String in, int count, char pad) {
        if (in.length() >= count)
            return in;

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count - in.length(); i++) {
            sb.append(pad);
        }

        for (char c : in.toCharArray()) {
            sb.append(c);
        }

        return sb.toString();
    }
}

Related

  1. leftPad(StringBuilder pStringBuilder, int pLength, char pChar)
  2. leftPadByteArray(byte[] source, int size)
  3. leftPadded(String src, int len)
  4. leftPaddedString(String string, int length, char paddingChar)
  5. leftPadding(final StringBuffer strBuf, final int bufLen, final char fill)
  6. leftPadding(String orgStr, String addStr, int strLength)
  7. leftPadInt(int number, int width)
  8. leftPadMultiline(String input, char padChar, int padWidth)
  9. leftPadString(String str, char pad, int length)