Java String Pad Left leftPad(String string, char pad, int size)

Here you can find the source of leftPad(String string, char pad, int size)

Description

Return the padded left of a string by padding character.

License

Open Source License

Parameter

Parameter Description
string a parameter
pad a parameter
size a parameter

Declaration

public static String leftPad(String string, char pad, int size) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*w  ww .  j av a2 s . c om*/
     * Return the padded left of a string by padding character.
     *
     * @param string
     * @param pad
     * @param size
     * @return
     */
    public static String leftPad(String string, char pad, int size) {
        if (string == null || size < 0) {
            return null;
        } else if (string.length() == 0 || size == 0) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < size; i++) {
            sb.append(pad);
        }
        sb.append(string);
        int lengthOfInterimString = sb.length();
        return sb.toString().substring(lengthOfInterimString - size, lengthOfInterimString);
    }
}

Related

  1. leftPad(String str, int size, String padStr)
  2. leftPad(String str, int size, String padStr)
  3. leftPad(String str, int width, char c)
  4. leftPad(String str, int width, char padding)
  5. leftPad(String str, String character, int size)
  6. leftPad(String string, int length)
  7. leftPad(String strInput, int intLength)
  8. leftPad(String targetStr, char appendChar, int length)
  9. leftPad(String text, int length, char padChar)