Java String Pad Right rightPad(String srcStr, char padChar, int destLen)

Here you can find the source of rightPad(String srcStr, char padChar, int destLen)

Description

right Pad

License

Open Source License

Declaration

public final static String rightPad(String srcStr, char padChar, int destLen) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {

    public final static String rightPad(String srcStr, char padChar, int destLen) {

        if (srcStr == null) {
            return genStr(padChar, destLen);
        } else if (srcStr.length() < destLen) {
            StringBuilder destSrc = new StringBuilder(destLen);
            destSrc.append(srcStr);//w w w . j  a v  a 2s . c o  m
            for (int i = srcStr.length(); i < destLen; i++) {
                destSrc.append(padChar);
            }
            return destSrc.toString();
        } else {
            return srcStr;
        }

    }

    private static String genStr(char padChar, int len) {
        StringBuilder str = new StringBuilder(len);
        for (int i = 0; i < len; i++) {
            str.append(padChar);
        }
        return str.toString();
    }
}

Related

  1. rightPad(String s, int minLength)
  2. rightPad(String s, int minLength, char filling)
  3. rightPad(String s, int n)
  4. rightPad(String s, int width)
  5. rightPad(String s, int z)
  6. rightPad(String srcString, char c, int length)
  7. rightPad(String str, int len, char c)
  8. rightPad(String str, int length, char padding)
  9. rightPad(String str, int max, char pad)