Here you can find the source of leftPad(String s, int z)
Parameter | Description |
---|---|
s | The String to pad out |
z | The size to pad to |
public static String leftPad(String s, int z)
//package com.java2s; //License from project: Open Source License public class Main { /**/*from w ww.j a v a 2s . co m*/ * Left pad a String with spaces. Pad to a size of n. * * @param s * The String to pad out * @param z * The size to pad to * @return The left padded String */ public static String leftPad(String s, int z) { return leftPad(s, z, " "); } /** * Left 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 * String to pad with * @return left padded String */ public static String leftPad(String s, int z, String d) { z = (z - s.length()) / d.length(); if (z > 0) s = repeat(d, z) + s; 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); } }