Java tutorial
//package com.java2s; import java.util.Arrays; public class Main { public static String padString(String s, int length) { return padString(s, ' ', length); } public static String padString(String s, char padChar, int length) { int slen, numPads = 0; if (s == null) { s = ""; numPads = length; } else if ((slen = s.length()) > length) { s = s.substring(0, length); } else if (slen < length) { numPads = length - slen; } if (numPads == 0) { return s; } char[] c = new char[numPads]; Arrays.fill(c, padChar); return s + new String(c); } }