Here you can find the source of left(String s, int width)
Parameter | Description |
---|---|
s | the string to justify |
width | the field width to justify within |
public static String left(String s, int width)
//package com.java2s; /******************************************************************************* * Copyright (c) 2009, 2014 IBM Corp.// ww w. j a v a 2s.c o m * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * and Eclipse Distribution License v1.0 which accompany this distribution. * * The Eclipse Public License is available at * http://www.eclipse.org/legal/epl-v10.html * and the Eclipse Distribution License is available at * http://www.eclipse.org/org/documents/edl-v10.php. * *******************************************************************************/ public class Main { /** * Left justify a string, padding with spaces. * * @param s the string to justify * @param width the field width to justify within * * @return the justified string. */ public static String left(String s, int width) { return left(s, width, ' '); } /** * Left justify a string. * * @param s the string to justify * @param width the field width to justify within * @param fillChar the character to fill with * * @return the justified string. */ public static String left(String s, int width, char fillChar) { if (s.length() >= width) { return s; } StringBuffer sb = new StringBuffer(width); sb.append(s); for (int i = width - s.length(); --i >= 0;) { sb.append(fillChar); } return sb.toString(); } }