Java String Pad Right rightPad(String ret, int limit)

Here you can find the source of rightPad(String ret, int limit)

Description

Right pad a string: adds spaces to a string until a certain length.

License

Apache License

Parameter

Parameter Description
ret The string to pad
limit The desired length of the padded string.

Return

The padded String.

Declaration

public static final String rightPad(String ret, int limit) 

Method Source Code

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

public class Main {
    /**//from  w ww .j  a  va 2  s  .  com
     * Right pad a string: adds spaces to a string until a certain length. If
     * the length is smaller then the limit specified, the String is truncated.
     * 
     * @param ret
     *            The string to pad
     * @param limit
     *            The desired length of the padded string.
     * @return The padded String.
     */
    public static final String rightPad(String ret, int limit) {
        if (ret == null)
            return rightPad(new StringBuffer(), limit);
        else
            return rightPad(new StringBuffer(ret), limit);
    }

    /**
     * Right pad a StringBuffer: adds spaces to a string until a certain length.
     * If the length is smaller then the limit specified, the String is
     * truncated.
     * 
     * @param ret
     *            The StringBuffer to pad
     * @param limit
     *            The desired length of the padded string.
     * @return The padded String.
     */
    public static final String rightPad(StringBuffer ret, int limit) {
        int len = ret.length();
        int l;

        if (len > limit) {
            ret.setLength(limit);
        } else {
            for (l = len; l < limit; l++)
                ret.append(' ');
        }
        return ret.toString();
    }
}

Related

  1. rightPad(String input, char padding, int length)
  2. rightPad(String input, int length, char pad)
  3. rightPad(String inStr, int length, char paddingChar)
  4. rightPad(String original, int length, char padChar)
  5. rightPad(String originalText, int length, char fillChar)
  6. rightPad(String s, int length)
  7. rightPad(String s, int length)
  8. rightPad(String s, int length)
  9. rightPad(String s, int minLength)