Java String Pad Left lpad(String str, int totalPadAmount, String padChar)

Here you can find the source of lpad(String str, int totalPadAmount, String padChar)

Description

Pad to the left of a string for totalPadAmount using padChar

License

Open Source License

Parameter

Parameter Description
str - string to pad
totalPadAmount - the amount to pad
padChar - the character to pad with e.g. " "

Return

String

Declaration

public static String lpad(String str, int totalPadAmount, String padChar) 

Method Source Code

//package com.java2s;
/**//from   w  w w . j av  a 2s. c o  m
 * (c) 2014 Cisco and/or its affiliates. All rights reserved.
 * 
 * This software is released under the Eclipse Public License. The details can be found in the file LICENSE. 
 * Any dependent libraries supplied by third parties are provided under their own open source licenses as 
 * described in their own LICENSE files, generally named .LICENSE.txt. The libraries supplied by Cisco as 
 * part of the Composite Information Server/Cisco Data Virtualization Server, particularly csadmin-XXXX.jar, 
 * csarchive-XXXX.jar, csbase-XXXX.jar, csclient-XXXX.jar, cscommon-XXXX.jar, csext-XXXX.jar, csjdbc-XXXX.jar, 
 * csserverutil-XXXX.jar, csserver-XXXX.jar, cswebapi-XXXX.jar, and customproc-XXXX.jar (where -XXXX is an 
 * optional version number) are provided as a convenience, but are covered under the licensing for the 
 * Composite Information Server/Cisco Data Virtualization Server. They cannot be used in any way except 
 * through a valid license for that product.
 * 
 * This software is released AS-IS!. Support for this software is not covered by standard maintenance agreements with Cisco. 
 * Any support for this software by Cisco would be covered by paid consulting agreements, and would be billable work.
 * 
 */

public class Main {
    /**
     * Pad to the left of a string for totalPadAmount using padChar
     * @param str - string to pad
     * @param totalPadAmount - the amount to pad
     * @param padChar - the character to pad with e.g. " "
     * @return String
     */
    public static String lpad(String str, int totalPadAmount, String padChar) {
        // Pad a string with spaces starting on the left
        String padStr = str;
        if (padStr.length() < totalPadAmount) {
            String pad = "";
            for (int i = padStr.length(); i < totalPadAmount; i++) {
                pad = pad + padChar;
            }
            padStr = pad + padStr;
        }
        return padStr;
    }
}

Related

  1. lPad(String str, int len, char pad)
  2. lpad(String str, int len, char padding)
  3. LPad(String str, int length, String chr)
  4. lPad(String str, int length, String padString)
  5. lpad(String str, int size)
  6. lpad(String str, String chr, int length)
  7. lPad(String string, char[] padding, int length)
  8. lPad(String target, String fix, int length)
  9. lPad(String val, int length, String padChar)