public static String rpad ( String base , int len , String pad ) { if ( len < 0 || "" . equals ( pad ) ) { return null ; } else if ( len == 0 ) { return "" ; } char [ ] data = new char [ len ] ; char [ ] baseChars = base . toCharArray ( ) ; char [ ] padChars = pad . toCharArray ( ) ; int pos = 0 ; // copy the base while ( pos < base . length ( ) && pos < len ) { data [ pos ] = baseChars [ pos ] ; pos += 1 ; } // copy the padding while ( pos < len ) { int i = 0 ; while ( i < pad . length ( ) && i < len - pos ) { data [ pos + i ] = padChars [ i ] ; i += 1 ; } pos += pad . length ( ) ; } return new String ( data ) ; }
Returns the string str right - padded with the string pad to a length of len characters . If str is longer than len the return value is shortened to len characters .