Java String Pad Left leftPad(Object obj, char pad, int len)

Here you can find the source of leftPad(Object obj, char pad, int len)

Description

left Pad

License

Apache License

Declaration

public static String leftPad(Object obj, char pad, int len) 

Method Source Code

//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

public class Main {
    public static String leftPad(Object obj, char pad, int len) {
        if (obj == null)
            return null;

        String str = obj.toString();

        int padLen = len - str.length();

        if (padLen <= 0)
            return str;

        StringBuilder b = new StringBuilder();

        for (int i = 0; i < padLen; i++)
            b.append(pad);/*from   www.  j  a  v  a2  s .  co m*/

        b.append(str);

        return b.toString();
    }

    public static final String toString(Object obj) {
        return obj == null || isBlank(obj.toString()) ? null : obj.toString().trim();
    }

    public static boolean isBlank(String value) {
        return value == null || value.trim().length() == 0;
    }

    public static String trim(String str, String trim) {
        int start = 0;

        while (str.startsWith(trim, start))
            start += trim.length();

        str = str.substring(start);

        while (str.endsWith(trim))
            str = str.substring(0, str.length() - trim.length());

        return str;
    }
}

Related

  1. leftPad(final String input, final int size)
  2. leftPad(final String numStr, final int numDigits)
  3. leftPad(final String str, final int size, String padStr)
  4. leftPad(final String value, final int length, final char paddingChar)
  5. leftPad(long value, char padChar, int maxDigits, StringBuilder buf)
  6. leftPad(String _str, int _size, char _padChar)
  7. leftPad(String aStr, int aLen)
  8. leftPad(String base, int length, char pad)
  9. leftPad(String csIn, int nRequiredLength, char cFill)