Java - Write code to Return a copy of s padded with trailing spaces so that it's length is length.

Requirements

Write code to Return a copy of s padded with trailing spaces so that it's length is length.

Demo

//package com.book2s;

public class Main {
    public static void main(String[] argv) {
        String s = "book2s.com";
        int length = 42;
        System.out.println(rightPad(s, length));
    }/*from  w  ww . j  av a  2  s  .co  m*/

    /**
     * Returns a copy of <code>s</code> padded with trailing spaces so
     * that it's length is <code>length</code>.  Strings already
     * <code>length</code> characters long or longer are not altered.
     */
    public static String rightPad(String s, int length) {
        StringBuffer sb = new StringBuffer(s);
        for (int i = length - s.length(); i > 0; i--)
            sb.append(" ");
        return sb.toString();
    }
}