Java String Left Justify leftJustify(long v, int width)

Here you can find the source of leftJustify(long v, int width)

Description

The leftJustify() method pads a string to a specified length by adding spaces on the right, thus justifying the string to the left margin.

License

Open Source License

Parameter

Parameter Description
v a long value to convert to a string and justify
width the number of characters to pad the string to

Return

a string representation of the input, padded on the right with spaces to achieve the desired length.

Declaration

public static String leftJustify(long v, int width) 

Method Source Code

//package com.java2s;

public class Main {
    /**//w ww. java 2s .  com
     * The <code>leftJustify()</code> method pads a string to a specified length by adding spaces on the
     * right, thus justifying the string to the left margin. This is extremely useful in generating columnar
     * output in textual tables.
     *
     * @param v     a long value to convert to a string and justify
     * @param width the number of characters to pad the string to
     * @return a string representation of the input, padded on the right with spaces to achieve the desired
     *         length.
     */
    public static String leftJustify(long v, int width) {
        return leftJustify(Long.toString(v), width);
    }

    /**
     * The <code>leftJustify()</code> method pads a string to a specified length by adding spaces on the
     * right, thus justifying the string to the left margin. This is extremely useful in generating columnar
     * output in textual tables.
     *
     * @param v     a floating point value to convert to a string and justify
     * @param width the number of characters to pad the string to
     * @return a string representation of the input, padded on the right with spaces to achieve the desired
     *         length.
     */
    public static String leftJustify(float v, int width) {
        return leftJustify(Float.toString(v), width);
    }

    /**
     * The <code>leftJustify()</code> method pads a string to a specified length by adding spaces on the
     * right, thus justifying the string to the left margin. This is extremely useful in generating columnar
     * output in textual tables.
     *
     * @param s     a string to justify
     * @param width the number of characters to pad the string to
     * @return a string representation of the input, padded on the right with spaces to achieve the desired
     *         length.
     */
    public static String leftJustify(String s, int width) {
        StringBuffer buf = new StringBuffer(s);
        for (int pad = width - s.length(); pad > 0; pad--)
            buf.append(' ');
        return buf.toString();
    }
}

Related

  1. LeftAdjust(String s, int len, char pad)
  2. leftJustify(String s, int maxLength)
  3. leftJustify(String sourceString, int targetLen, char pad)
  4. leftJustify(String str, int width)
  5. leftJustify(String string, int width)