Java String Leading Character addLeadingCharacter(String s, char c, int len)

Here you can find the source of addLeadingCharacter(String s, char c, int len)

Description

Adds specified leading characters to the specified length.

License

Open Source License

Parameter

Parameter Description
s the source string.
c the leading character(s) to be added.
len the length of the target string.

Return

the string after adding the specified leading character(s).

Declaration

public static String addLeadingCharacter(String s, char c, int len) 

Method Source Code

//package com.java2s;
/* //w  w w . j  a v  a2  s  .  c  o 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 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. addLeadingCharacter(String s, char c, int len)
  2. addLeadingSlash(String fileName)
  3. addLeadingSlash(String fileName)
  4. addLeadingSlash(String path)