Pad the specified number of spaces to the input string to make it that length - Android java.lang

Android examples for java.lang:String Pad

Description

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

Demo Code

import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;

public class Main{

    /**//from   w  w w.j  av 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 Tutorials