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  w w . j a  v  a 2  s . c o m*/

    /**
     * 
     * @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();
    }
}