Java String Pad Left leftPad(String inStr, int length, char paddingChar)

Here you can find the source of leftPad(String inStr, int length, char paddingChar)

Description

Left padding the string with the specified padding character upto the specified length.

License

Open Source License

Parameter

Parameter Description
inStr Input string
length Padding length
paddingChar Padding character

Return

Padding string

Declaration

public static String leftPad(String inStr, int length, char paddingChar) 

Method Source Code

//package com.java2s;

public class Main {
    /**/*from  w  w  w .j a v a 2  s  .com*/
     * Left padding the string with the specified padding character upto the specified length.
     * @param inStr Input string
     * @param length Padding length
     * @param paddingChar Padding character
     * @return Padding string
     */
    public static String leftPad(String inStr, int length, char paddingChar) {
        if (inStr.length() == length)
            return inStr;

        StringBuffer outStr = new StringBuffer();
        for (int i = inStr.length(); i < length; i++) {
            outStr.append(paddingChar);
        }
        outStr.append(inStr);

        return outStr.toString();
    }

    /**
     * Converts object to a string.<br>
     * If object is null then return null
     * 
     * @param obj
     * @return
     */
    public static String toString(Object obj) {
        if (null == obj) {
            return null;
        } else {
            return obj.toString();
        }
    }
}

Related

  1. leftPad(String base, int length, char pad)
  2. leftPad(String csIn, int nRequiredLength, char cFill)
  3. leftPad(String in, char padding, int length)
  4. leftPad(String input, char padding, int length)
  5. leftPad(String input, int length, char pad)
  6. leftPad(String original, int length, char padChar)
  7. leftPad(String s, char paddingCharacter, int length)
  8. leftPad(String s, int l)
  9. leftPad(String s, int len, char c)