Here you can find the source of lpad(String s, int width)
Parameter | Description |
---|---|
s | The string to pad. |
width | The width to pad to. |
public static String lpad(String s, int width)
//package com.java2s; /* Please see the license information at the end of this file. */ public class Main { /** Left pad string with blanks to specified width. */*from w w w .ja v a2s . com*/ * @param s The string to pad. * @param width The width to pad to. * * @return "s" padded with enough blanks on left * to be "width" columns wide. */ public static String lpad(String s, int width) { if (s.length() < width) { return dupl(' ', width - s.length()) + s; } else { return s; } } /** Duplicate character into string. * * @param ch The character to be duplicated. * @param n The number of duplicates desired. * * @return String containing "n" copies of "ch". * * <p> * if n <= 0, the empty string "" is returned. * </p> */ public static String dupl(char ch, int n) { if (n > 0) { StringBuffer result = new StringBuffer(n); for (int i = 0; i < n; i++) { result.append(ch); } return result.toString(); } else { return ""; } } /** Duplicate string into string. * * @param s The string to be duplicated. * @param n The number of duplicates desired. * * @return String containing "n" copies of "s". * * <p> * if n <= 0, the empty string "" is returned. * </p> */ public static String dupl(String s, int n) { if (n > 0) { StringBuffer result = new StringBuffer(n); for (int i = 0; i < n; i++) { result.append(s); } return result.toString(); } else { return ""; } } }