Java String Align Right alignRight(String val, char pad, int width)

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

Description

align Right

License

Open Source License

Declaration

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

Method Source Code

//package com.java2s;
/* ************************************************************************
#
#  DivConq//from  www . j a v a2 s.  c  om
#
#  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 alignRight(String val, char pad, int width) {
        if (val.length() > width) {
            val = val.substring(0, width - 1) + "*";
            return val;
        }

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

        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. alignRight(String data)
  2. alignRight(String str, int length, boolean isEllipsis)
  3. alignRight(String str, int size, char padChar)
  4. alignRight(String substring, int totalWidth, char fill)
  5. alignRight(String text, int length)