StringUtil.java Source code

Java tutorial

Introduction

Here is the source code for StringUtil.java

Source

/*
 * Copyright 2000,2005 wingS development team.
 *
 * This file is part of wingS (http://wingsframework.org).
 *
 * wingS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * Please see COPYING for the complete licence.
 */

import java.util.StringTokenizer;

/**
 * Some string manipulation utilities.
 *
 * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
 */
public class StringUtil {

    private final static char[] ALPHAS = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', };

    /**
     * All possible digits for representing a number as a String
     * This is conservative and does not include 'special'
     * characters since some browsers don't handle them right.
     * The IE for instance seems to be case insensitive in class
     * names for CSSs. Grrr.
     */
    private final static char[] DIGITS = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd',
            'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y',
            'z',
            /* This %@&!-IE is case insensitive for certain
             * URLs and IDs
             * 'A' , 'B' ,
             * 'C' , 'D' , 'E' , 'F' , 'G' , 'H' ,
             * 'I' , 'J' , 'K' , 'L' , 'M' , 'N' ,
             * 'O' , 'P' , 'Q' , 'R' , 'S' , 'T' ,
             * 'U' , 'V' , 'W' , 'X' , 'Y' , 'Z'
             */
    };

    public static final int MAX_RADIX = DIGITS.length;

    /**
     * Codes number up to radix 62.
     * Note, this method is only public for backward compatiblity. don't
     * use it.
     *
     * @param minDigits returns a string with a least minDigits digits
     */
    public static String toString(long i, int radix, int minDigits) {
        char[] buf = new char[65];

        radix = Math.min(Math.abs(radix), MAX_RADIX);
        minDigits = Math.min(buf.length - 1, Math.abs(minDigits));

        int charPos = buf.length - 1;

        boolean negative = (i < 0);
        if (negative) {
            i = -i;
        }

        while (i >= radix) {
            buf[charPos--] = DIGITS[(int) (i % radix)];
            i /= radix;
        }
        buf[charPos] = DIGITS[(int) i];

        // if minimum length of the result string is set, pad it with the
        // zero-representation (that is: '0')
        while (charPos > buf.length - minDigits)
            buf[--charPos] = DIGITS[0];

        if (negative) {
            buf[--charPos] = '-';
        }

        return new String(buf, charPos, buf.length - charPos);
    }

}