Here you can find the source of leftPad(String text, int size)
Parameter | Description |
---|---|
text | string to pad |
size | length of resulting string |
public static String leftPad(String text, int size)
//package com.java2s; // it under the terms of the GNU General Public License as published by public class Main { /**// w w w.j av a2 s .c o m Pad some text on the left (i.e., right-align it) until it's a specified width. If the text is already longer than the desired length, it is returned. @param text string to pad @param size length of resulting string @return the original text, padded on the left */ public static String leftPad(String text, int size) { int numSpaces = size - text.length(); if (numSpaces <= 0) return text; StringBuffer buf = new StringBuffer(size); for (int i = 0; i < numSpaces; i++) buf.append(' '); for (int i = numSpaces; i < size; i++) buf.append(text.charAt(i - numSpaces)); return buf.toString(); } }