Java String Pad Left leftPad(String s, int target)

Here you can find the source of leftPad(String s, int target)

Description

Node.js lol

License

Open Source License

Parameter

Parameter Description
s Base string.
target Minimum length of output.

Return

s if it is at least the size of target, or s prefixed by enough spaces to make it the size of target.

Declaration

public static String leftPad(String s, int target) 

Method Source Code

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

public class Main {
    /**/*  w ww  . j  a  v  a 2 s.  c om*/
     * Node.js lol
     *
     * @param s Base string.
     * @param target Minimum length of output.
     * @return {@code s} if it is at least the size of target, or {@code s} prefixed by enough spaces
     * to make it the size of target.
     */
    public static String leftPad(String s, int target) {
        return leftPad(s, target, ' ');
    }

    /**
     * Node.js lol
     *
     * @param s Base string.
     * @param target Minimum length of output.
     * @param pad Character to pad the left side of the string with.
     * @return {@code s} if it is at least the size of target, or {@code s} prefixed by enough pad
     * characters to make it the size of target.
     */
    public static String leftPad(String s, int target, char pad) {
        StringBuilder sb = new StringBuilder();
        for (int i = s.length(); i < target; i++) {
            sb.append(pad);
        }
        sb.append(s);
        return sb.toString();
    }
}

Related

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