Java String Pad Right rightPadded(String src, int len)

Here you can find the source of rightPadded(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 rightPadded(String src, int len) 

Method Source Code

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

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

    /**
     * Pad the src to make it [len] bytes with trailing last parameter.
     * 
     * @param src
     * @param len
     * @param padStr
     * @return
     */
    public static String rightPadded(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 src + iterateChar(padChar, len - srcLen);
    }

    /**
     * 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. rightPad(String toPad, int totalLength)
  2. rightPad(String value, int length)
  3. rightPad(String value, int makeLength, char paddingCharacter)
  4. rightPad(String value, int tamanho, String pad)
  5. rightPad(StringBuilder pStringBuilder, int pLength, char pChar)
  6. rightPaddedBaseString(String bases, int length)
  7. rightPaddedString(String string, int length, char paddingChar)
  8. rightPadding(int id, int value)
  9. rightPaddWithSpace(String pString_, int stellen)