Java String Pad Left leftPadded(String src, int len)

Here you can find the source of leftPadded(String src, int len)

Description

Pad the src to make it [len] bytes with trailing spaces.

License

Open Source License

Parameter

Parameter Description
src a parameter
len a parameter

Declaration

public static String leftPadded(String src, int len) 

Method Source Code

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

public class Main {
    /**/*from w w  w .  j  a v  a2  s . co m*/
     * Pad the src to make it [len] bytes with trailing spaces.
     * 
     * @param src
     * @param len
     * @return
     */
    public static String leftPadded(String src, int len) {
        return leftPadded(src, len, ' ');
    }

    /**
     * Pad the src to make it [len] bytes with trailing last parameter.
     * 
     * @param src
     * @param len
     * @param padChar
     * @return
     */
    public static String leftPadded(String src, int len, char padChar) {
        if (len < 1)
            return "";
        if (src == null)
            src = "";
        src = src.trim();
        int srcLen = src.length();
        if (srcLen > len)
            return src.substring(0, len);
        return iterateChar(padChar, len - srcLen) + src;
    }

    /**
     * Trims the character both from left and right
     * 
     * @param str
     * @param filler
     * @return
     */
    public static String trim(String str, final char filler) {
        // TODO: Performasi arttirilmali!!
        return rtrim(ltrim(str, filler), filler);
    }

    /**
     * Fills the string with the given char, as long as given length
     * 
     * @param ch
     * @param len
     * @return
     */
    public static String iterateChar(char ch, int len) {
        if (len < 1)
            return "";
        char buf[] = new char[len];
        for (int i = 0; i < len; i++)
            buf[i] = ch;
        return new String(buf);
    }

    /**
     * PErforms right trim
     * 
     * @param str
     * @param filler
     * @return
     */
    public static String rtrim(final String str, final char filler) {
        // TODO: performansi arttirilmali
        if (str == null)
            return null;
        for (int i = str.length() - 1; i >= 0; --i) {
            if (str.charAt(i) != filler)
                return str.substring(0, i + 1);
        }
        return "";
    }

    /**
     * Performs left trim
     * 
     * @param str
     * @param filler
     * @return
     */
    public static String ltrim(final String str, final char filler) {
        // TODO: Performasi arttirilmali!!
        if (str == null)
            return null;
        for (int i = 0; i < str.length(); ++i) {
            if (str.charAt(i) != filler) {
                return str.substring(i);
            }
        }
        return "";
    }
}

Related

  1. leftPad(String toPad, int numPads)
  2. leftPad(String value, int makeLength, char paddingCharacter)
  3. leftPad(String value, int size, String pad)
  4. leftPad(StringBuilder pStringBuilder, int pLength, char pChar)
  5. leftPadByteArray(byte[] source, int size)
  6. leftPaddedString(String string, int length, char paddingChar)
  7. leftPadding(final StringBuffer strBuf, final int bufLen, final char fill)
  8. leftPadding(String in, int count, char pad)
  9. leftPadding(String orgStr, String addStr, int strLength)