public static final String characterPad ( final String s , final int fieldLength , final char padChar , final boolean postpend ) { final char [ ] chArr = s . toCharArray ( ) ; final int sLen = chArr . length ; if ( sLen < fieldLength ) { final char [ ] out = new char [ fieldLength ] ; final int blanks = fieldLength - sLen ; if ( postpend ) { for ( int i = 0 ; i < sLen ; i ++ ) { out [ i ] = chArr [ i ] ; } for ( int i = sLen ; i < fieldLength ; i ++ ) { out [ i ] = padChar ; } } else { //prepend for ( int i = 0 ; i < blanks ; i ++ ) { out [ i ] = padChar ; } for ( int i = blanks ; i < fieldLength ; i ++ ) { out [ i ] = chArr [ i - blanks ] ; } } return String . valueOf ( out ) ; } return s ; }
Prepend or postpend the given string with the given character to fill the given field length . If the given string is equal or greater than the given field length it will be returned without modification .