Java String Pad Left leftPad(String srcString, char c, int length)

Here you can find the source of leftPad(String srcString, char c, int length)

Description

left Pad

License

LGPL

Declaration

public static String leftPad(String srcString, char c, int length) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

public class Main {
    public static String leftPad(String srcString, char c, int length) {
        if (srcString == null) {
            srcString = "";
        }//from www  .  ja  va  2 s  .c o m
        int tLen = srcString.length();

        if (tLen >= length)
            return srcString;
        int iMax = length - tLen;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < iMax; i++) {
            sb.append(c);
        }
        sb.append(srcString);
        return sb.toString();
    }

    public static int length(String str) {
        if (str == null)
            return 0;
        char[] c = str.toCharArray();
        int len = 0;
        for (int i = 0; i < c.length; i++) {
            len++;
            if (!isLetter(c[i])) {
                len++;
            }
        }
        return len;
    }

    private static boolean isLetter(char charStr) {
        int k = 0x80;
        return charStr / k == 0 ? true : false;
    }
}

Related

  1. leftPad(String s, int n)
  2. leftPad(String s, int target)
  3. leftPad(String s, int width)
  4. leftPad(String s, int z)
  5. leftPad(String srcStr, char padChar, int destLen)
  6. leftPad(String srcString, char c, int length)
  7. leftPad(String str, int len, char c)
  8. leftPad(String str, int length)
  9. leftPad(String str, int length, char c)