Here you can find the source of rightPad(String str, int size)
public static String rightPad(String str, int size)
//package com.java2s; /*/*from ww w. j a va 2 s. c o m*/ * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * <p> * http://www.apache.org/licenses/LICENSE-2.0 * <p> * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ public class Main { public static String rightPad(String str, int size) { return rightPad(str, size, ' '); } public static String rightPad(String str, int size, char padChar) { if (str == null) { return null; } else { int pads = size - str.length(); return pads <= 0 ? str : (pads > 8192 ? rightPad(str, size, String.valueOf(padChar)) : str.concat(repeat(padChar, pads))); } } public static String rightPad(String str, int size, String padStr) { if (str == null) { return null; } else { if (isEmpty(padStr)) { padStr = " "; } int padLen = padStr.length(); int strLen = str.length(); int pads = size - strLen; if (pads <= 0) { return str; } else if (padLen == 1 && pads <= 8192) { return rightPad(str, size, padStr.charAt(0)); } else 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)); } } } public static String repeat(char ch, int repeat) { char[] buf = new char[repeat]; for (int i = repeat - 1; i >= 0; --i) { buf[i] = ch; } return new String(buf); } public static boolean isEmpty(CharSequence cs) { return cs == null || cs.length() == 0; } }