* If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @return padded String. * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String prepad(String s, int length){ return prepad(s, length, ' '); } /** * Pre-pend the given character to the String until * the result is the desired length. *
* If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @param c padding character. * @return padded String. * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String prepad(String s, int length, char c){ int needed = length - s.length(); if (needed <= 0){ return s; } char padding[] = new char[needed]; java.util.Arrays.fill(padding, c); StringBuffer sb = new StringBuffer(length); sb.append(padding); sb.append(s); return sb.toString(); } /** * Pad the end of the given String with spaces until * the String is of the given length. *
* If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @return padded String. * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String postpad(String s, int length){ return postpad(s, length, ' '); } /** * Append the given character to the String until * the result is the desired length. *
* If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @param c padding character. * @return padded String. * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String postpad(String s, int length, char c){ int needed = length - s.length(); if (needed <= 0){ return s; } char padding[] = new char[needed]; java.util.Arrays.fill(padding, c); StringBuffer sb = new StringBuffer(length); sb.append(s); sb.append(padding); return sb.toString(); } /** * Pad the beginning and end of the given String with spaces until * the String is of the given length. The result is that the original * String is centered in the middle of the new string. *
* If the number of characters to pad is even, then the padding * will be split evenly between the beginning and end, otherwise, * the extra character will be added to the end. *
* If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @return padded String. * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String midpad(String s, int length){ return midpad(s, length, ' '); } /** * Pad the beginning and end of the given String with the given character * until the result is the desired length. The result is that the original * String is centered in the middle of the new string. *
* If the number of characters to pad is even, then the padding * will be split evenly between the beginning and end, otherwise, * the extra character will be added to the end. *
* If a String is longer than the desired length, * it will not be truncated, however no padding * will be added. * * @param s String to be padded. * @param length desired length of result. * @param c padding character. * @return padded String. * @throws NullPointerException if s is null. * * @since ostermillerutils 1.00.00 */ public static String midpad(String s, int length, char c){ int needed = length - s.length(); if (needed <= 0){ return s; } int beginning = needed / 2; int end = beginning + needed % 2; char prepadding[] = new char[beginning]; java.util.Arrays.fill(prepadding, c); char postpadding[] = new char[end]; java.util.Arrays.fill(postpadding, c); StringBuffer sb = new StringBuffer(length); sb.append(prepadding); sb.append(s); sb.append(postpadding); return sb.toString(); } }