Here you can find the source of addLeadingSpace(String s, int len)
Parameter | Description |
---|---|
s | the source string. |
len | the length of the target string. |
public static String addLeadingSpace(String s, int len)
//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; } }