Java String Pad Left leftPad(String str, int length, char c)

Here you can find the source of leftPad(String str, int length, char c)

Description

left Pad

License

Apache License

Declaration

private static String leftPad(String str, int length, char c) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    private static String leftPad(String str, int length, char c) {
        if (str.length() >= length) {
            return str;
        }/*from  w w  w.j  a  v  a  2  s. c  o m*/

        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < length - str.length(); i++) {
            buf.append(c);
        }
        buf.append(str);
        return buf.toString();
    }

    public static String toString(Object[] objArr) {
        if (objArr == null) {
            return null;
        }

        StringBuffer buf = new StringBuffer("[");
        for (int i = 0; i < objArr.length; i++) {
            buf.append((i > 0 ? "," : "") + objArr[i]);
        }
        buf.append("]");
        return buf.toString();
    }

    public static String toString(Object obj) {
        if (obj instanceof String) {
            return "\"" + obj + "\"";
        }
        if (obj instanceof Object[]) {
            return toString((Object[]) obj);
        } else {
            return String.valueOf(obj);
        }
    }
}

Related

  1. leftPad(String srcStr, char padChar, int destLen)
  2. leftPad(String srcString, char c, int length)
  3. leftPad(String srcString, char c, int length)
  4. leftPad(String str, int len, char c)
  5. leftPad(String str, int length)
  6. leftPad(String str, int length, char padding)
  7. leftPad(String str, int maxLength, char placeholder)
  8. leftPad(String str, int num, String padStr)
  9. leftPad(String str, int pad)