Java - Write code to fill String from Right with Blank

Requirements

Write code to fill String from Right with Blank

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String str = "book2s.com";
        int len = 42;
        System.out.println(StringFillRightBlank(str, len));
    }// w w w .  j a va  2s . c  o  m

    public static String StringFillRightBlank(String str, int len) {
        if (str.length() < len) {
            StringBuffer sb = new StringBuffer(len);
            sb.append(str);
            for (int i = 0; i < len - str.length(); i++)
                sb.append(' ');
            return new String(sb);
        } else
            return str;
    }
}