String pad Right - Java java.lang

Java examples for java.lang:String Pad

Description

String pad Right

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String string = "java2s.com";
        int totalWidth = 42;
        System.out.println(padRight(string, totalWidth));
    }/*from  w w w.  j a v  a2 s .c  om*/

    public static String padRight(String string, int totalWidth) {
        return padRight(string, totalWidth, ' ');
    }

    public static String padRight(String string, int totalWidth,
            char paddingChar) {
        StringBuilder sb = new StringBuilder(string);

        while (sb.length() < totalWidth) {
            sb.append(paddingChar);
        }

        return sb.toString();
    }
}

Related Tutorials