Here you can find the source of toPaddedString(String source, int length, String pad, boolean trailing)
public static String toPaddedString(String source, int length, String pad, boolean trailing)
//package com.java2s; public class Main { public static String toPaddedString(String source, int length, char pad, boolean trailing) { int len = source.length(); if (len >= length) { return source; }/*from w w w. java 2 s.c o m*/ StringBuffer sb = new StringBuffer(length); if (trailing) { sb.append(source); } for (int i = len; i < length; i++) { sb.append(pad); } if (!trailing) { sb.append(source); } return sb.toString(); } public static String toPaddedString(String source, int length, String pad, boolean trailing) { int len = source.length(); if (len == length) { return source; } if (len > length) { if (trailing) { return source.substring(0, length); } else { return source.substring(len - length, len); } } StringBuffer sb = new StringBuffer(length); int padLength = source.length(); int partLength = (length - padLength) % pad.length(); if (trailing) { sb.append(source); sb.append(pad.substring(pad.length() - partLength, pad.length())); } for (; padLength + pad.length() <= length; padLength += pad .length()) { sb.append(pad); } if (!trailing) { sb.append(pad.substring(0, partLength)); sb.append(source); } return sb.toString(); } }