Android String Pad from Left padLeft(String input, int size)

Here you can find the source of padLeft(String input, int size)

Description

Pad the specified number of spaces to the input string to make it that length

Parameter

Parameter Description
input a parameter
size a parameter

Declaration

public static String padLeft(String input, int size) 

Method Source Code

//package com.java2s;

public class Main {
    /**//w  w w . ja  v  a2s  .c o m
     * Pad the specified number of spaces to the input string to make it that length
     * @param input
     * @param size
     * @return
     */
    public static String padLeft(String input, int size) {

        if (input.length() > size) {
            throw new IllegalArgumentException(
                    "input must be shorter than or equal to the number of spaces: "
                            + size);
        }

        StringBuilder sb = new StringBuilder();
        for (int i = input.length(); i < size; i++) {
            sb.append(" ");
        }
        return sb.append(input).toString();
    }
}

Related

  1. lpad(String str, int length)
  2. padding(int width)
  3. prefixPad(String toPad, String padLetter, int targetLength)