Here you can find the source of rightPadString(StringBuilder builder, char padding, int multipleOf)
Parameter | Description |
---|---|
builder | Builder to pad. |
padding | Padding character. |
multipleOf | Number which the length must be a multiple of. |
Parameter | Description |
---|---|
IllegalArgumentException | if builder is null or multipleOf is less than2. |
static void rightPadString(StringBuilder builder, char padding, int multipleOf)
//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); } } } }