Java String Pad Right rightPad(String inStr, int length, char paddingChar)

Here you can find the source of rightPad(String inStr, int length, char paddingChar)

Description

Right padding the string with the specified padding character upto the specified length.

License

Open Source License

Parameter

Parameter Description
inStr Input string
length Padding length
paddingChar Padding character

Return

Padding string

Declaration

public static String rightPad(String inStr, int length, char paddingChar) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  w  w .java2s .  com
     * Right padding the string with the specified padding character upto the specified length.
     * @param inStr Input string
     * @param length Padding length
     * @param paddingChar Padding character
     * @return Padding string
     */
    public static String rightPad(String inStr, int length, char paddingChar) {
        if (inStr.length() == length)
            return inStr;

        StringBuffer outStr = new StringBuffer();
        outStr.append(inStr);

        for (int i = inStr.length(); i < length; i++) {
            outStr.append(paddingChar);
        }

        return outStr.toString();
    }

    /**
     * Converts object to a string.<br>
     * If object is null then return null
     * 
     * @param obj
     * @return
     */
    public static String toString(Object obj) {
        if (null == obj) {
            return null;
        } else {
            return obj.toString();
        }
    }
}

Related

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