Java String Pad Right rightPad(String in, char padding, int length)

Here you can find the source of rightPad(String in, char padding, int length)

Description

right Pad

License

Open Source License

Declaration

public static String rightPad(String in, char padding, int length) 

Method Source Code

//package com.java2s;
/*/*www  . ja  va 2s .com*/
GPON General Purpose Object Network
Copyright (C) 2006 Daniel Schulz
    
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
    
This library 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
Lesser General Public License for more details.
    
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

public class Main {
    public static String rightPad(String in, char padding, int length) {
        String out = "";

        int toPad = length - in.length();

        if (toPad < 0) {
            throw new IllegalArgumentException("Input string longer than requested length");
        }

        out = in + getStringRepetition(padding + "", toPad);

        return out;
    }

    public static String getStringRepetition(String in, int times) {
        StringBuffer out = new StringBuffer("");

        for (int i = 0; i < times; i++) {
            out = out.append(in);
        }

        return out.toString();
    }
}

Related

  1. rightPad(final String input, final int size)
  2. rightPad(final String str, final int size, final char padChar)
  3. rightPad(String _str, int _size, char _padChar)
  4. rightPad(String csIn, int nRequiredLength, char cFill)
  5. rightPad(String field, char padding, int width)
  6. rightPad(String in, int len, String pad)
  7. rightPad(String input, char padding, int length)
  8. rightPad(String input, int length, char pad)
  9. rightPad(String inStr, int length, char paddingChar)