Java String Leading Character addLeadingSpace(String s, int len)

Here you can find the source of addLeadingSpace(String s, int len)

Description

Adds leading spaces to the given string to the specified length.

License

Open Source License

Parameter

Parameter Description
s the source string.
len the length of the target string.

Return

the string after adding leading spaces.

Declaration

public static String addLeadingSpace(String s, int len) 

Method Source Code

//package com.java2s;
/* //  w w  w.j a  v a  2 s  .co m
 * Copyright(c) 2005 Center for E-Commerce Infrastructure Development, The
 * University of Hong Kong (HKU). All Rights Reserved.
 *
 * This software is licensed under the GNU GENERAL PUBLIC LICENSE Version 2.0 [1]
 * 
 * [1] http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
 */

public class Main {
    /**
     * Adds leading spaces to the given string to the specified length. Nothing
     * will be done if the length of the given string is equal to or greater
     * than the specified length.
     * 
     * @param s the source string.
     * @param len the length of the target string.
     * @return the string after adding leading spaces.
     */
    public static String addLeadingSpace(String s, int len) {
        return addLeadingCharacter(s, ' ', len);
    }

    /**
     * Adds specified leading characters to the specified length. Nothing will
     * be done if the length of the given string is equal to or greater than the
     * specified length.
     * 
     * @param s the source string.
     * @param c the leading character(s) to be added.
     * @param len the length of the target string.
     * @return the string after adding the specified leading character(s).
     */
    public static String addLeadingCharacter(String s, char c, int len) {
        while (s != null && s.length() < len) {
            s = c + s;
        }
        return s;
    }
}

Related

  1. addLeadingSlash(String fileName)
  2. addLeadingSlash(String fileName)
  3. addLeadingSlash(String path)
  4. addLeadingSlash(String path)
  5. addLeadingSlash(String string)
  6. addLeadingSpace(String s, int len)
  7. addLeadingUnixSlash(String fileName)
  8. addLeadingZero(byte[] value)
  9. addLeadingZero(final int value)