Java String Pad Right rightPad(String s, int z)

Here you can find the source of rightPad(String s, int z)

Description

Right pad a String with spaces.

License

Open Source License

Parameter

Parameter Description
s String to repeat
z int number of times to repeat

Return

right padded String

Declaration

public static String rightPad(String s, int z) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**/*  w  w w . jav  a2s.  c  o  m*/
     * Right pad a String with spaces. Pad to a size of n.
     * 
     * @param s
     *            String to repeat
     * @param z
     *            int number of times to repeat
     * @return right padded String
     */
    public static String rightPad(String s, int z) {
        return rightPad(s, z, " ");
    }

    /**
     * Right pad a String with a specified string. Pad to a size of n.
     * 
     * @param s
     *            The string to pad out
     * @param z
     *            The size to pad to
     * @param d
     *            The string to pad with
     * @return The right padded String
     */
    public static String rightPad(String s, int z, String d) {
        z = (z - s.length()) / d.length();
        if (z > 0)
            s += repeat(d, z);
        return s;
    }

    /**
     * Repeat a string n times to form a new string.
     * 
     * @param s
     *            String to repeat
     * @param t
     *            The times of the string to repeat
     * @return The new string after repeat
     */
    public static String repeat(String s, int t) {
        StringBuffer buffer = new StringBuffer(t * s.length());

        for (int i = 0; i < t; i++)
            buffer.append(s);

        return buffer.toString();
    }

    public static String toString(int[] codePoints, int offset, int count) {
        if (offset < 0) {
            throw new StringIndexOutOfBoundsException(offset);
        }
        if (count < 0) {
            throw new StringIndexOutOfBoundsException(count);
        }
        // Note: offset or count might be near -1>>>1.
        if (offset > codePoints.length - count) {
            throw new StringIndexOutOfBoundsException(offset + count);
        }

        int expansion = 0;
        int margin = 1;
        char[] v = new char[count + margin];
        int x = offset;
        int j = 0;
        for (int i = 0; i < count; i++) {
            int c = codePoints[x++];
            if (c < 0) {
                throw new IllegalArgumentException();
            }
            if (margin <= 0 && (j + 1) >= v.length) {
                if (expansion == 0) {
                    expansion = (((-margin + 1) * count) << 10) / i;
                    expansion >>= 10;
                    if (expansion <= 0) {
                        expansion = 1;
                    }
                } else {
                    expansion *= 2;
                }
                int newLen = Math.min(v.length + expansion, count * 2);
                margin = (newLen - v.length) - (count - i);

                char[] copy = new char[newLen];
                System.arraycopy(v, 0, copy, 0, Math.min(v.length, newLen));
                v = copy;
            }
            if (c < 0x010000) {
                v[j++] = (char) c;
            } else if (c <= 0x10ffff) {

                // Character.toSurrogates(c, v, j);
                int charOffset = c - 0x010000;
                v[j + 1] = (char) ((charOffset & 0x3ff) + '\uDC00');
                v[j] = (char) ((charOffset >>> 10) + '\uD800');

                j += 2;
                margin--;
            } else {
                throw new IllegalArgumentException();
            }
        }

        return new String(v, 0, j);
    }
}

Related

  1. rightPad(String s, int length)
  2. rightPad(String s, int minLength)
  3. rightPad(String s, int minLength, char filling)
  4. rightPad(String s, int n)
  5. rightPad(String s, int width)
  6. rightPad(String srcStr, char padChar, int destLen)
  7. rightPad(String srcString, char c, int length)
  8. rightPad(String str, int len, char c)
  9. rightPad(String str, int length, char padding)