Java String Left Justify LeftAdjust(String s, int len, char pad)

Here you can find the source of LeftAdjust(String s, int len, char pad)

Description

Pad the string s on the right side with pad until len characters.

License

Open Source License

Parameter

Parameter Description
s String to adjust
len Number of characters after the adjustment
pad Pad/fill character

Return

The adjusted string padded with the pad character until it is len characters long

Declaration

public static String LeftAdjust(String s, int len, char pad) 

Method Source Code

//package com.java2s;
/* jvmtest - Testing your VM 
  Copyright (C) 20009, Guenther Wimpassinger
    //from  w w  w  .ja  v  a2s .  c  o m
  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.
    
  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.
    
  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

public class Main {
    /**
     * Pad the string <code>s</code> on the right side with <code>pad</code>
     * until <code>len</code> characters. If <code>s</code> is
     * longer the right part is truncated.
     * @param s String to adjust
     * @param len Number of characters after the adjustment
     * @param pad Pad/fill character
     * @return The adjusted string padded with the <code>pad</code>
     * character until it is <code>len</code> characters long
     */
    public static String LeftAdjust(String s, int len, char pad) {
        char[] ca = new char[len];
        int k;
        if (s == null) {
            k = 0;
        } else {
            k = s.length();
        }

        for (int i = 0; i < len && i < k; i++) {
            ca[i] = s.charAt(i);
        }
        for (int i = k; i < len; i++) {
            ca[i] = pad;
        }

        return new String(ca, 0, ca.length);
    }
}

Related

  1. leftJustify(long v, int width)
  2. leftJustify(String s, int maxLength)
  3. leftJustify(String sourceString, int targetLen, char pad)
  4. leftJustify(String str, int width)