right Pad - Java java.lang

Java examples for java.lang:String Pad

Description

right Pad

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String input = "java2s.com";
        char padding = 'a';
        int length = 42;
        System.out.println(rightPad(input, padding, length));
    }//w  ww .  ja v  a 2s  . c o m

    public static String rightPad(String input, char padding, int length) {
        if (nullOrEmpty(input))
            input = "";
        if (input.length() >= length)
            return input;
        String padString = createPad(padding, length - input.length());
        return input.concat(padString);
    }

    public static boolean nullOrEmpty(String s) {
        return (s == null) ? true : "".equals(s.trim());
    }

    public static String createPad(char padding, int length) {
        StringBuilder out = new StringBuilder();
        for (int repeat = 0; repeat < length; repeat++)
            out.append(padding);
        return out.toString();
    }

    public static String trim(String s) {
        return emptyIfNull(s).trim();
    }

    public static String emptyIfNull(String s) {
        return (s == null) ? "" : s;
    }
}

Related Tutorials