Java String Left Justify leftJustifyString(String s, int width)

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

Description

Left justify a string in a fixed-width field, using blanks for padding.

License

BSD License

Parameter

Parameter Description
s the string
width the desired field width

Return

a left-justified version of the string

Declaration

public static String leftJustifyString(String s, int width) 

Method Source Code

//package com.java2s;
/*---------------------------------------------------------------------------*\
  $Id: a2e18c1fd38fa21f86084fbdb2c0045cec86bd20 $
  ---------------------------------------------------------------------------
  This software is released under a BSD-style license:
    /*from  w w w. j  a v a 2s  .c  o m*/
  Copyright (c) 2004-2007 Brian M. Clapper. All rights reserved.
    
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:
    
  1.  Redistributions of source code must retain the above copyright notice,
  this list of conditions and the following disclaimer.
    
  2.  The end-user documentation included with the redistribution, if any,
  must include the following acknowlegement:
    
"This product includes software developed by Brian M. Clapper
(bmc@clapper.org, http://www.clapper.org/bmc/). That software is
copyright (c) 2004-2007 Brian M. Clapper."
    
  Alternately, this acknowlegement may appear in the software itself,
  if wherever such third-party acknowlegements normally appear.
    
  3.  Neither the names "clapper.org", "clapper.org Java Utility Library",
  nor any of the names of the project contributors may be used to
  endorse or promote products derived from this software without prior
  written permission. For written permission, please contact
  bmc@clapper.org.
    
  4.  Products derived from this software may not be called "clapper.org
  Java Utility Library", nor may "clapper.org" appear in their names
  without prior written permission of Brian M. Clapper.
    
  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN
  NO EVENT SHALL BRIAN M. CLAPPER BE LIABLE FOR ANY DIRECT, INDIRECT,
  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\*---------------------------------------------------------------------------*/

public class Main {
    /**
     * Left justify a string in a fixed-width field, using blanks for
     * padding. If the string is already longer than the field width, it is
     * returned unchanged.
     *
     * @param s      the string
     * @param width  the desired field width
     *
     * @return a left-justified version of the string
     *
     * @see #leftJustifyString(String,int,char)
     * @see #rightJustifyString(String,int)
     * @see #centerString(String,int)
     */
    public static String leftJustifyString(String s, int width) {
        return leftJustifyString(s, width, ' ');
    }

    /**
     * Left justify a string in a fixed-width field, using the specified
     * character for padding. If the string is already longer than the
     * field width, it is returned unchanged.
     *
     * @param s      the string
     * @param width  the desired field width
     * @param c      the pad character
     *
     * @return a left-justified version of the string
     *
     * @see #leftJustifyString(String,int)
     * @see #rightJustifyString(String,int,char)
     * @see #centerString(String,int,char)
     */
    public static String leftJustifyString(String s, int width, char c) {
        StringBuilder paddedString = new StringBuilder(width);
        int paddingNeeded;
        int len = s.length();

        paddingNeeded = (width < len) ? 0 : (width - len);
        paddedString.append(s);

        for (int i = 0; i < paddingNeeded; i++)
            paddedString.append(c);

        return paddedString.toString();
    }
}

Related

  1. leftJustify(String s, int maxLength)
  2. leftJustify(String sourceString, int targetLen, char pad)
  3. leftJustify(String str, int width)
  4. leftJustify(String string, int width)
  5. leftJustify(String value, int width)
  6. leftJustifyText(String originalValue, int length, char padCharacter)