Java String Align Left alignLeft(String str, int size)

Here you can find the source of alignLeft(String str, int size)

Description

align Left

License

Open Source License

Declaration

public static String alignLeft(String str, int size) 

Method Source Code

//package com.java2s;

public class Main {

    public static String alignLeft(String str, int size) {
        return alignLeft(str, size, ' ');
    }//ww w  . ja v a 2  s.com

    public static String alignLeft(String str, int size, char padChar) {
        if (str == null) {
            return null;
        }

        int pads = size - str.length();

        if (pads <= 0) {
            return str;
        }

        return alignLeft(str, size, String.valueOf(padChar));
    }

    public static String alignLeft(String str, int size, String padStr) {
        if (str == null) {
            return null;
        }

        if ((padStr == null) || (padStr.length() == 0)) {
            padStr = " ";
        }

        int padLen = padStr.length();
        int strLen = str.length();
        int pads = size - strLen;

        if (pads <= 0) {
            return str;
        }

        if (pads == padLen) {
            return str.concat(padStr);
        } else if (pads < padLen) {
            return str.concat(padStr.substring(0, pads));
        } else {
            char[] padding = new char[pads];
            char[] padChars = padStr.toCharArray();

            for (int i = 0; i < pads; i++) {
                padding[i] = padChars[i % padLen];
            }

            return str.concat(new String(padding));
        }
    }
}

Related

  1. alignLeft(CharSequence cs, int width, char c)
  2. alignLeft(String sLine, int iSize)
  3. alignLeft(String str, int length)
  4. alignLeft(String substring, int totalWidth, char fill)
  5. alignLeft(String val, char pad, int width)