Java - Write code to left Fill Char to a string

Requirements

Write code to left Fill Char to a string

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String val = "book2s.com";
        char c = 'a';
        int maxlen = 42;
        System.out.println(leftFillChar(val, c, maxlen));
    }/*from w ww  . j a va2 s.  c om*/

    /**
     * 
     * @param val
     * @param c
     * @param maxlen
     * @return
     */
    public static String leftFillChar(String val, char c, int maxlen) {
        StringBuffer sb = new StringBuffer();
        if (val.length() < maxlen) {
            for (int i = 0; i < maxlen - val.length(); i++) {
                sb.append('0');
            }
        }
        sb.append(val);
        return sb.toString();
    }
}