Java String Left left(String s, int width)

Here you can find the source of left(String s, int width)

Description

Left justify a string, padding with spaces.

License

Open Source License

Parameter

Parameter Description
s the string to justify
width the field width to justify within

Return

the justified string.

Declaration

public static String left(String s, int width) 

Method Source Code

//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();
    }
}

Related

  1. left(String baseString, int pos)
  2. left(String s, int l)
  3. left(String s, int len)
  4. left(String s, int nCaratteri)
  5. left(String s, int size)
  6. left(String source, int length)
  7. left(String source, String searchFor)
  8. left(String source, String searchFor)
  9. left(String str, int count)