String pad Left - Java java.lang

Java examples for java.lang:String Pad

Description

String pad Left

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        String string = "java2s.com";
        int totalWidth = 42;
        System.out.println(padLeft(string, totalWidth));
    }/* www  .  jav a 2  s. c  om*/

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

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

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

        sb.append(string);
        return sb.toString();
    }
}

Related Tutorials