Java - Write code to to Pad String

Requirements

Write code to to Pad String

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String source = "book2s.com";
        int length = 42;
        char pad = 'a';
        boolean trailing = true;
        System.out.println(toPaddedString(source, length, pad, trailing));
    }/*w ww  .java  2 s.c o  m*/

    public static String toPaddedString(String source, int length,
            char pad, boolean trailing) {

        int len = source.length();

        if (len >= length) {
            return source;
        }

        StringBuffer sb = new StringBuffer(length);

        if (trailing) {
            sb.append(source);
        }

        for (int i = len; i < length; i++) {
            sb.append(pad);
        }

        if (!trailing) {
            sb.append(source);
        }

        return sb.toString();
    }
}