Java String Pad Right rightPadString(StringBuilder builder, char padding, int multipleOf)

Here you can find the source of rightPadString(StringBuilder builder, char padding, int multipleOf)

Description

Pad a StringBuilder to a desired multiple on the right using a specified character.

License

Apache License

Parameter

Parameter Description
builder Builder to pad.
padding Padding character.
multipleOf Number which the length must be a multiple of.

Exception

Parameter Description
IllegalArgumentException if builder is null or multipleOf is less than2.

Declaration

static void rightPadString(StringBuilder builder, char padding, int multipleOf) 

Method Source Code

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

public class Main {
    /**/*from   w w  w  .ja v a  2 s  .  com*/
     * Pad a {@link StringBuilder} to a desired multiple on the right using a specified character.
     *
     * @param builder Builder to pad.
     * @param padding Padding character.
     * @param multipleOf Number which the length must be a multiple of.
     * @throws IllegalArgumentException if {@code builder} is null or {@code multipleOf} is less than
     * 2.
     */
    static void rightPadString(StringBuilder builder, char padding, int multipleOf) {
        if (builder == null) {
            throw new IllegalArgumentException("Builder input must not be empty.");
        }
        if (multipleOf < 2) {
            throw new IllegalArgumentException("Multiple must be greater than one.");
        }
        int needed = multipleOf - (builder.length() % multipleOf);
        if (needed < multipleOf) {
            for (int i = needed; i > 0; i--) {
                builder.append(padding);
            }
        }
    }
}

Related

  1. rightPadding(int id, int value)
  2. rightPaddWithSpace(String pString_, int stellen)
  3. rightPadInt(int number, int width)
  4. rightPadOrTruncate(String str, int resultSz)
  5. rightPadString(String str, char pad, int length)
  6. tailPad(String target, String padChar, int length)