⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stringtools.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
                        return new Long(this.numStr);                    } else                    if (BigInteger.class.equals(this.type)) {                        return new BigInteger(this.numStr);                    } else                    if (Float.class.equals(this.type)) {                        return new Float(this.numStr);                    } else                    if (Double.class.equals(this.type)) {                        return new Double(this.numStr);                    } else {                        Print.logError("Unkrecognized Number type: " + StringTools.className(this.type));                        return dft;                    }                } catch (NumberFormatException nfe) {                    // should not occur                    Print.logException("Number conversion error", nfe);                    return dft;                }            }            return dft;        }                public String toString() {            StringBuffer sb = new StringBuffer();            sb.append(StringTools.quoteString(this.getInputString()));            sb.append("/");            sb.append(this.getClassTypeName());            sb.append("/");            sb.append(this.getStart());            sb.append("/");            sb.append(this.getEnd());            return sb.toString();        }            }        // ------------------------------------------------------------------------    /**    *** Parse the specified object into a boolean value    *** @param data  The object to parse    *** @param dft   The default boolean value if unable to parse the specified object    *** @return The parsed boolean value    **/    public static boolean parseBoolean(Object data, boolean dft)    {        if (data == null) {            return dft;        } else        if (data instanceof Boolean) {            return ((Boolean)data).booleanValue();        } else {            return StringTools.parseBoolean(data.toString(), dft);        }    }        /**    *** Parse the specified String into a boolean value    *** @param data  The String to parse    *** @param dft   The default boolean value if unable to parse the specified object    *** @return The parsed boolean value    **/    public static boolean parseBoolean(String data, boolean dft)    {        if (data != null) {            String v = data.toLowerCase();            if (dft) {                // if default is 'true', only test for 'false'                for (int i = 0; i < BooleanFALSE.length; i++) {                    if (v.startsWith(BooleanFALSE[i])) {                        return false;                    }                }            } else {                // if default is 'false', only test for 'true'                for (int i = 0; i < BooleanTRUE.length; i++) {                    if (v.startsWith(BooleanTRUE[i])) {                        return true;                    }                }            }            // else return default            return dft;        }        return dft;    }    /**    *** Return true if the specified String contains a valid boolean value    *** @param data  The String to test    *** @param strict True to test for a strict boolean value (ie. does not contain    ***               any other superfluous trailing characters), false to allow for     ***               other non-critical trailing characters.    *** @return True if the specified String contains a valid boolean value    **/    public static boolean isBoolean(String data, boolean strict)    {        String v = data.toLowerCase();        for (int i = 0; i < BooleanTRUE.length; i++) {            boolean ok = strict? v.equals(BooleanTRUE[i]) : v.startsWith(BooleanTRUE[i]);            if (ok) {                return true;            }        }        for (int i = 0; i < BooleanFALSE.length; i++) {            boolean ok = strict? v.equals(BooleanFALSE[i]) : v.startsWith(BooleanFALSE[i]);            if (ok) {                return true;            }        }        return false;    }    // ------------------------------------------------------------------------    /**    *** Parse the specified String into a Dimension object    *** @param data  The String object to parse    *** @param dft   The default Dimension object if unable to parse the String    *** @return The parsed Dimension object    **/    public static Dimension parseDimension(String data, Dimension dft)    {        int p = (data != null)? data.indexOf("/") : -1;        if (p > 0) {            int w = StringTools.parseInt(data.substring(0,p),0);            int h = StringTools.parseInt(data.substring(p+1),0);            return new Dimension(w, h);        } else {            return dft;        }    }        // ------------------------------------------------------------------------    public static final String HEX = "0123456789ABCDEF";        /**     *** Returns the value of the specified hex character    *** @param ch  The hex character to return the value    *** @return The value of the specified hex character, or -1 if the specified    ***         character is not a valid hex character    **/    public static int hexIndex(char ch)    {        return StringTools.HEX.indexOf(Character.toUpperCase(ch));    }        /**    *** Returns the hex character for the least significant nybble of the specified byte    *** @param nybble The value to convert to a hex character.  Only the least significant    ***               nybble of this byte will be used to convert to the hex character.    *** @return The character representation of the specifified nybble    **/    public static char hexNybble(byte nybble)    {        return HEX.charAt(nybble & 0xF);    }        /**    *** Returns the hex character for the least significant nybble of the specified byte    *** @param nybble The value to convert to a hex character.  Only the least significant    ***               nybble of this byte will be used to convert to the hex character.    *** @return The character representation of the specifified nybble    **/    public static char hexNybble(int nybble)    {        return HEX.charAt(nybble & 0xF);    }    /**    *** Parse the specified String, containing a hex representation, into a byte array    *** @param data  The String containing the hex character values    *** @param dft   The default byte array return if unable to convert the specified String value    *** @return The parse byte array    **/    public static byte[] parseHex(String data, byte dft[])    {        if (data != null) {                        /* get data string */            String d = data.toUpperCase();            String s = d.startsWith("0X")? d.substring(2) : d;                        /* remove any invalid trailing characters */            // scan until we find an invalid character (or the end of the string)            for (int i = 0; i < s.length(); i++) {                if (HEX.indexOf(s.charAt(i)) < 0) {                    s = s.substring(0, i);                    break;                }            }                        /* return default if nothing to parse */            if (s.equals("")) {                return dft;             }                        /* right justify */            if ((s.length() & 1) == 1) { s = "0" + s; } // right justified                        /* parse data */            byte rtn[] = new byte[s.length() / 2];            for (int i = 0; i < s.length(); i += 2) {                int c1 = HEX.indexOf(s.charAt(i));                if (c1 < 0) { c1 = 0; /* Invalid Hex char */ }                int c2 = HEX.indexOf(s.charAt(i+1));                if (c2 < 0) { c2 = 0; /* Invalid Hex char */ }                rtn[i/2] = (byte)(((c1 << 4) & 0xF0) | (c2 & 0x0F));            }                        /* return value */            return rtn;                    } else {                        return dft;                    }    }        /**    *** Parse the String containing a hex representation into an int value    *** @param data The String hex representation to convert to a String    *** @param dft  The default int value to return if unable to convert the specified String hex representation.    *** @return The parse int value    **/    public static int parseHex(String data, int dft)    {        return (int)StringTools.parseHexLong(data, (long)dft);    }        /**    *** Parse the String containing a hex representation into an int value    *** @param data The String hex representation to convert to a String    *** @param dft  The default int value to return if unable to convert the specified String hex representation.    *** @return The parse int value    **/    public static int parseHexInt(String data, int dft)    {        return (int)StringTools.parseHexLong(data, (long)dft);    }        /**    *** Parse the String containing a hex representation into a long value    *** @param data The String hex representation to convert to a String    *** @param dft  The default long value to return if unable to convert the specified String hex representation.    *** @return The parse long value    **/    public static long parseHex(String data, long dft)    {        return StringTools.parseHexLong(data, dft);    }        /**    *** Parse the String containing a hex representation into a long value    *** @param data The String hex representation to convert to a String    *** @param dft  The default long value to return if unable to convert the specified String hex representation.    *** @return The parse long value    **/    public static long parseHexLong(String data, long dft)    {        byte b[] = parseHex(data, null);        if (b != null) {            long val = 0L;            for (int i = 0; i < b.length; i++) {                val = (val << 8) | ((int)b[i] & 0xFF);            }            return val;        } else {            return dft;        }    }        /**    *** Returns the number of valid hex characters found in the specified String    *** @param data  The String containing the hex representation    *** @return The number of valid hex characters    **/    public static int hexLength(String data)    {        if (StringTools.isBlank(data)) {            return 0;        } else {            String d = data.toUpperCase();            int s = d.startsWith("0X")? 2 : 0, e = s;            for (; (e < d.length()) && (HEX.indexOf(d.charAt(e)) >= 0); e++);            return e;        }    }    /* return true if the specified string represents a valid hex value */    /**    *** Returns true if the specified String contains hext characters    *** @param data  The String representation of the hex characters to test    *** @param strict  True to check for strict hex character values, false to allow for    ***                trailing superfluous characters.    *** @return True if the specified String contains a valie hex representation, false otherwise.    **/    public static boolean isHex(String data, boolean strict)    {        if (StringTools.isBlank(data)) {            return false;        } else {            String d = data.toUpperCase();            int s = d.startsWith("0X")? 2 : 0, e = s;            for (; e < d.length(); e++) {                if (HEX.indexOf(d.charAt(e)) < 0) {                    if (strict) {                        return false;                    } else {                        break;                    }                }            }            return (e > s);        }    }        // ------------------------------------------------------------------------        /**    *** This method prints the specified byte array to a String hex representation    *** showing the contained bytes with corresponding displayed printable characters.    *** @param b  The byte array to convert to a String representation    *** @return The hex representation in the form of a StringBuffer    **/    public static StringBuffer formatHexString(byte b[])    {        return StringTools.formatHexString(b, 0, -1, 16, true, null);    }        /**    *** This method prints the specified byte array to a String hex representation    *** showing the contained bytes with corresponding displayed printable characters.    *** @param b  The byte array to convert to a String representation    *** @param blockLen  The number of bytes display on a single row.    *** @return The hex representation in the form of a StringBuffer    **/    public static StringBuffer formatHexString(byte b[], int blockLen)    {        return StringTools.formatHexString(b, 0, -1, blockLen, true, null);    }    /**    *** This method prints the specified byte array to a String hex representation    *** showing the contained bytes with co

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -