Java String Align Left alignLeft(String val, char pad, int width)

Here you can find the source of alignLeft(String val, char pad, int width)

Description

align Left

License

Open Source License

Declaration

public static String alignLeft(String val, char pad, int width) 

Method Source Code

//package com.java2s;
/* ************************************************************************
#
#  DivConq//w ww. j a  v a2  s .  c o  m
#
#  http://divconq.com/
#
#  Copyright:
#    Copyright 2014 eTimeline, LLC. All rights reserved.
#
#  License:
#    See the license.txt file in the project's top-level directory for details.
#
#  Authors:
#    * Andy White
#
************************************************************************ */

public class Main {
    public static String alignLeft(String val, char pad, int width) {
        if (val == null)
            val = "";

        if (val.length() > width) {
            val = "*" + val.substring(0, width - 1);
            return val;
        }

        while (val.length() < width)
            val = val + pad;

        return val;
    }

    /**
     * Gets a CharSequence length or {@code 0} if the CharSequence is
     * {@code null}.
     *
     * @param cs
     *            a CharSequence or {@code null}
     * @return CharSequence length or {@code 0} if the CharSequence is
     *         {@code null}.
     * @since 2.4
     * @since 3.0 Changed signature from length(String) to length(CharSequence)
     */
    public static int length(final CharSequence cs) {
        return cs == null ? 0 : cs.length();
    }
}

Related

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