Java String Leading Character addLeadingZeros(String deweyCallNum)

Here you can find the source of addLeadingZeros(String deweyCallNum)

Description

adds leading zeros to a dewey call number, when they're missing.

License

Open Source License

Parameter

Parameter Description
deweyCallNum a parameter

Return

the dewey call number with leading zeros

Declaration

public static String addLeadingZeros(String deweyCallNum) 

Method Source Code

//package com.java2s;

public class Main {
    /**//  w  w w.ja v a 2 s  .  c  om
     * adds leading zeros to a dewey call number, when they're missing.
     *
     * @param deweyCallNum
     * @return the dewey call number with leading zeros
     */
    public static String addLeadingZeros(String deweyCallNum) {
        String result = deweyCallNum;
        String b4Cutter = getPortionBeforeCutter(deweyCallNum);

        // TODO: could call Utils.normalizeFloat(b4Cutter.trim(), 3, -1);
        // but still need to add back part after cutter

        String b4dec = null;
        int decIx = b4Cutter.indexOf(".");
        if (decIx >= 0)
            b4dec = deweyCallNum.substring(0, decIx).trim();
        else
            b4dec = b4Cutter.trim();

        if (b4dec != null) {
            switch (b4dec.length()) {
            case 1:
                result = "00" + deweyCallNum;
                break;
            case 2:
                result = "0" + deweyCallNum;
            }
        }

        return result;
    }

    /**
     * return the portion of the call number string that occurs before the
     * Cutter, NOT including any class suffixes occuring before the cutter
     */
    public static final String getPortionBeforeCutter(String callnum) {

        // cutter is a single letter followed by digits.
        // there may be a space before a cutter
        // there should be a period, which is followed by a single letter
        //   the period is sometimes missing
        // For Dewey callnumber, there may be a slash instead of a cutter,
        //  or there might be NO cutter
        String beginCutterRegex = "( +|(\\.[A-Z])| */)";

        String[] pieces = callnum.split(beginCutterRegex);
        if (pieces.length == 0 || pieces[0] == null
                || pieces[0].length() == 0)
            return null;
        else
            return pieces[0].trim();
    }
}

Related

  1. addLeadingZero(byte[] value)
  2. addLeadingZero(final int value)
  3. addLeadingZero(String aString, int aAmountOfZeros)
  4. addLeadingZeroesTo16BitBinaryString(String binaryValue)
  5. addLeadingZeros(Object number, int places)
  6. addLeadingZeros(String number, int maxDigits)
  7. addLeadingZeros(String s, int nZeros)
  8. addLeadingZeros(String str)
  9. addLeadingZerosToNumber(long number, int length)