Here you can find the source of rightPad(String str, int size, char padChar)
public static String rightPad(String str, int size, char padChar)
//package com.java2s; //License from project: Open Source License public class Main { private static final int PAD_LIMIT = 8192; public static String rightPad(String str, int size, char padChar) { if (str == null) { return null; }/*from w w w . j a v a 2 s . com*/ int pads = size - str.length(); if (pads <= 0) { return str; // returns original String when possible } if (pads > PAD_LIMIT) { return rightPad(str, size, String.valueOf(padChar)); } return str.concat(padding(pads, padChar)); } public static String rightPad(String str, int size, String padStr) { if (str == null) { return null; } if (isEmpty(padStr)) { padStr = " "; } int padLen = padStr.length(); int strLen = str.length(); int pads = size - strLen; if (pads <= 0) { return str; // returns original String when possible } if (padLen == 1 && pads <= PAD_LIMIT) { return rightPad(str, size, padStr.charAt(0)); } if (pads == padLen) { return str.concat(padStr); } else if (pads < padLen) { return str.concat(padStr.substring(0, pads)); } else { char[] padding = new char[pads]; char[] padChars = padStr.toCharArray(); for (int i = 0; i < pads; i++) { padding[i] = padChars[i % padLen]; } return str.concat(new String(padding)); } } private static String padding(int repeat, char padChar) throws IndexOutOfBoundsException { if (repeat < 0) { throw new IndexOutOfBoundsException("Cannot pad a negative amount: " + repeat); } final char[] buf = new char[repeat]; for (int i = 0; i < buf.length; i++) { buf[i] = padChar; } return new String(buf); } /** * Helper function for null and empty string testing. * * @return true iff s == null or s.equals(""); */ public static boolean isEmpty(String s) { return makeSafe(s).length() == 0; } /** * Helper function for making null strings safe for comparisons, etc. * * @return (s == null) ? "" : s; */ public static String makeSafe(String s) { return (s == null) ? "" : s; } }