Java String Pad Zero zeroPadLeft(String s, final int len)

Here you can find the source of zeroPadLeft(String s, final int len)

Description

zero Pad Left

License

Open Source License

Parameter

Parameter Description
s a parameter
len a parameter

Declaration

public static String zeroPadLeft(String s, final int len) 

Method Source Code

//package com.java2s;

public class Main {
    public static final String STR_EMP = "";
    private static final char[][] ZERO_POOL = new char[65][];

    /**//from   w w w . ja v a2  s  . c o  m
     * 
     * @param s
     * @param len
     * @return
     */
    public static String zeroPadLeft(String s, final int len) {
        s = s.trim();
        if (s.length() > len) {
            return s;
        }
        int fll = len - s.length();
        final char[] charArray = new char[len];
        int index = 0, res = fll / 65;
        while (res > 0) {
            System.arraycopy(ZERO_POOL[64], 0, charArray, index, 64);
            index += 65;
            res--;
        }
        res = fll;
        fll = fll % 65;
        if (fll > 0) {
            System.arraycopy(ZERO_POOL[fll], 0, charArray, index, ZERO_POOL[fll].length);
        }
        s.getChars(0, s.length(), charArray, res);
        return new String(charArray);
    }

    /**
     * 
     * @param s
     * @param c
     * @return
     */
    public static String trim(final String s, final String c) {
        final int length = s.length();
        if (c == null) {
            return s;
        }
        final int cLength = c.length();
        if (c.length() == 0) {
            return s;
        }
        int start = 0;
        int end = length;
        boolean found = false;
        int i;
        for (i = 0; !found && i < length; i++) {
            char ch = s.charAt(i);
            found = true;
            for (int j = 0; found && j < cLength; j++) {
                if (c.charAt(j) == ch) {
                    found = false;
                }
            }
        }
        if (!found) {
            return STR_EMP;
        }
        start = i - 1;
        found = false;
        for (i = length - 1; !found && i >= 0; i--) {
            char ch = s.charAt(i);
            found = true;
            for (int j = 0; found && j < cLength; j++) {
                if (c.charAt(j) == ch) {
                    found = false;
                }
            }
        }
        end = i + 2;
        return s.substring(start, end);
    }

    /**
     * 
     * @param s
     * @param c
     * @return
     */
    public static String trim(final String s, final char c) {
        final int length = s.length();
        int cLength = 1;
        int start = 0;
        int end = length;
        boolean found = false;
        int i;
        for (i = 0; !found && i < length; i++) {
            char ch = s.charAt(i);
            found = true;
            for (int j = 0; found && j < cLength; j++) {
                if (c == ch) {
                    found = false;
                }
            }
        }
        if (!found) {
            return STR_EMP;
        }
        start = i - 1;
        found = false;
        for (i = length - 1; !found && i >= 0; i--) {
            char ch = s.charAt(i);
            found = true;
            for (int j = 0; found && j < cLength; j++) {
                if (c == ch) {
                    found = false;
                }
            }
        }
        end = i + 2;
        return s.substring(start, end);
    }
}

Related

  1. zeroPad(String s, int length)
  2. zeroPad(String string, int digits)
  3. zeroPadAfterTo(String hex, int i)
  4. zeroPadding(String str, int len)
  5. zeroPadding(String value, int n)
  6. zeropadRight(String s, int len)
  7. zeroPadSSN(String ssn)
  8. zeroPadString(String string, int length)
  9. zeroPadString(String string, int length)