Java String Pad Right rightPad(String original, int length, char padChar)

Here you can find the source of rightPad(String original, int length, char padChar)

Description

Pads the String with the given character until it has the given length.

License

Open Source License

Declaration

public static String rightPad(String original, int length, char padChar) 

Method Source Code

//package com.java2s;
/*//from  w w w . j av a 2s  .  co  m
 * Copyright (c) 2016 Vivid Solutions.
 *
 * 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 v. 1.0 which accompanies 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 {
    /**
     *  Pads the String with the given character until it has the given length. If
     *  original is longer than the given length, returns original.
     */
    public static String rightPad(String original, int length, char padChar) {
        if (original.length() >= length) {
            return original;
        }
        return original + stringOfChar(padChar, length - original.length());
    }

    /**
     *  Returns a String of the given length consisting entirely of the given
     *  character
     */
    public static String stringOfChar(char ch, int count) {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < count; i++) {
            buf.append(ch);
        }
        return buf.toString();
    }
}

Related

  1. rightPad(String in, char padding, int length)
  2. rightPad(String in, int len, String pad)
  3. rightPad(String input, char padding, int length)
  4. rightPad(String input, int length, char pad)
  5. rightPad(String inStr, int length, char paddingChar)
  6. rightPad(String originalText, int length, char fillChar)
  7. rightPad(String ret, int limit)
  8. rightPad(String s, int length)
  9. rightPad(String s, int length)