Here you can find the source of leftPad(String s, int target)
Parameter | Description |
---|---|
s | Base string. |
target | Minimum length of output. |
public static String leftPad(String s, int target)
//package com.java2s; //License from project: Open Source License public class Main { /**/* w ww . j a v a 2 s. c om*/ * Node.js lol * * @param s Base string. * @param target Minimum length of output. * @return {@code s} if it is at least the size of target, or {@code s} prefixed by enough spaces * to make it the size of target. */ public static String leftPad(String s, int target) { return leftPad(s, target, ' '); } /** * Node.js lol * * @param s Base string. * @param target Minimum length of output. * @param pad Character to pad the left side of the string with. * @return {@code s} if it is at least the size of target, or {@code s} prefixed by enough pad * characters to make it the size of target. */ public static String leftPad(String s, int target, char pad) { StringBuilder sb = new StringBuilder(); for (int i = s.length(); i < target; i++) { sb.append(pad); } sb.append(s); return sb.toString(); } }