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

📄 stringutils.java

📁 在资料浩瀚的互联网中
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
     * @param offset where to start converting from     * @param length how many characters to convert.     *     * @return byte[] with 0x5c escaped     */    public static byte[] escapeEasternUnicodeByteStream(byte[] origBytes,        String origString, int offset, int length) {        if ((origBytes == null) || (origBytes.length == 0)) {            return origBytes;        }        int bytesLen = origBytes.length;        int bufIndex = 0;        int strIndex = 0;        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(bytesLen);        while (true) {            if (origString.charAt(strIndex) == '\\') {                // write it out as-is                bytesOut.write(origBytes[bufIndex++]);                //bytesOut.write(origBytes[bufIndex++]);            } else {                // Grab the first byte                int loByte = origBytes[bufIndex];                if (loByte < 0) {                    loByte += 256; // adjust for signedness/wrap-around                }                // We always write the first byte                bytesOut.write(loByte);                //                // The codepage characters in question exist between                // 0x81-0x9F and 0xE0-0xFC...                //                // See:                //                // http://www.microsoft.com/GLOBALDEV/Reference/dbcs/932.htm                //                // Problematic characters in GBK                //                // U+905C : CJK UNIFIED IDEOGRAPH                //                // Problematic characters in Big5                //                // B9F0 = U+5C62 : CJK UNIFIED IDEOGRAPH                //                if (loByte >= 0x80) {                    if (bufIndex < (bytesLen - 1)) {                        int hiByte = origBytes[bufIndex + 1];                        if (hiByte < 0) {                            hiByte += 256; // adjust for signedness/wrap-around                        }                        // write the high byte here, and increment the index                        // for the high byte                        bytesOut.write(hiByte);                        bufIndex++;                        // escape 0x5c if necessary                        if (hiByte == 0x5C) {                            bytesOut.write(hiByte);                        }                    }                } else if (loByte == 0x5c) {                    if (bufIndex < (bytesLen - 1)) {                        int hiByte = origBytes[bufIndex + 1];                        if (hiByte < 0) {                            hiByte += 256; // adjust for signedness/wrap-around                        }                        if (hiByte == 0x62) {                            // we need to escape the 0x5c                            bytesOut.write(0x5c);                            bytesOut.write(0x62);                            bufIndex++;                        }                    }                }                bufIndex++;            }            if (bufIndex >= bytesLen) {                // we're done                break;            }            strIndex++;        }        return bytesOut.toByteArray();    }    /**     * Returns the first non whitespace char, converted to upper case     *     * @param searchIn the string to search in     *     * @return the first non-whitespace character, upper cased.     */    public static char firstNonWsCharUc(String searchIn) {        if (searchIn == null) {            return 0;        }        int length = searchIn.length();        for (int i = 0; i < length; i++) {            char c = searchIn.charAt(i);            if (!Character.isWhitespace(c)) {                return Character.toUpperCase(c);            }        }        return 0;    }    /**     * DOCUMENT ME!     *     * @param searchIn DOCUMENT ME!     * @param searchFor DOCUMENT ME!     *     * @return DOCUMENT ME!     */    public static int indexOfIgnoreCase(String searchIn, String searchFor) {        if ((searchIn == null) || (searchFor == null)) {            return -1;        }        int patternLength = searchFor.length();        int stringLength = searchIn.length();        int stopSearchingAt = stringLength - patternLength;                int i = 0;        if (patternLength == 0) {            return -1;        }        // Brute force string pattern matching        // Some locales don't follow upper-case rule, so need to check both        char firstCharOfPatternUc = Character.toUpperCase(searchFor.charAt(0));        char firstCharOfPatternLc = Character.toLowerCase(searchFor.charAt(0));        lookForFirstChar:         while (true) {            while ((i < stopSearchingAt) &&                    (Character.toUpperCase(searchIn.charAt(i)) != firstCharOfPatternUc) &&					 Character.toLowerCase(searchIn.charAt(i)) != firstCharOfPatternLc) {                i++;            }            if (i > stopSearchingAt) {                return -1;            }            int j = i + 1;            int end = (j + patternLength) - 1;            int k = 1; // start at second char of pattern            while (j < end) {            	int searchInPos = j++;            	int searchForPos = k++;            	                if (Character.toUpperCase(searchIn.charAt(searchInPos)) != Character.toUpperCase(                            searchFor.charAt(searchForPos))) {                    i++;                    // start over                    continue lookForFirstChar;                }                                // Georgian and Turkish locales don't have same convention, so need to check lowercase                // too!                if (Character.toLowerCase(searchIn.charAt(searchInPos)) != Character.toLowerCase(                        searchFor.charAt(searchForPos))) {                	i++;                	// start over                	continue lookForFirstChar;                }            }            return i; // found entire pattern        }    }    /**     * Determines whether or not the string 'searchIn' contains the string     * 'searchFor', dis-regarding case. Shorthand for a     * String.regionMatch(...)     *     * @param searchIn the string to search in     * @param searchFor the string to search for     *     * @return whether searchIn starts with searchFor, ignoring case     */    public static boolean startsWithIgnoreCase(String searchIn, String searchFor) {        return startsWithIgnoreCase(searchIn, 0, searchFor);    }    /**     * Determines whether or not the string 'searchIn' contains the string     * 'searchFor', dis-regarding case starting at 'startAt' Shorthand for a     * String.regionMatch(...)     *     * @param searchIn the string to search in     * @param startAt the position to start at     * @param searchFor the string to search for     *     * @return whether searchIn starts with searchFor, ignoring case     */    public static boolean startsWithIgnoreCase(String searchIn, int startAt,        String searchFor) {        return searchIn.regionMatches(true, 0, searchFor, startAt,            searchFor.length());    }    /**     * Determines whether or not the sting 'searchIn' contains the string     * 'searchFor', di-regarding case and leading whitespace     *     * @param searchIn the string to search in     * @param searchFor the string to search for     *     * @return true if the string starts with 'searchFor' ignoring whitespace     */    public static boolean startsWithIgnoreCaseAndWs(String searchIn,        String searchFor) {        if (searchIn == null) {            return searchFor == null;        }        int beginPos = 0;        int inLength = searchIn.length();        for (beginPos = 0; beginPos < inLength; beginPos++) {            if (!Character.isWhitespace(searchIn.charAt(beginPos))) {                break;            }        }        return startsWithIgnoreCase(searchIn, beginPos, searchFor);    }    /**     * Compares searchIn against searchForWildcard with wildcards  (heavily     * borrowed from strings/ctype-simple.c in the server sources)     *     * @param searchIn the string to search in     * @param searchForWildcard the string to search for, using the 'standard'     *        SQL wildcard chars of '%' and '_'     *     * @return WILD_COMPARE_MATCH_NO_WILD if matched, WILD_COMPARE_NO_MATCH if     *         not matched with wildcard,  WILD_COMPARE_MATCH_WITH_WILD if     *         matched with wildcard     */    public static int wildCompare(String searchIn, String searchForWildcard) {        if ((searchIn == null) || (searchForWildcard == null)) {            return WILD_COMPARE_NO_MATCH;        }        if (searchForWildcard.equals("%")) { //$NON-NLS-1$            return WILD_COMPARE_MATCH_WITH_WILD;        }        int result = WILD_COMPARE_NO_MATCH; /* Not found, using wildcards */        char wildcardMany = '%';        char wildcardOne = '_';        char wildcardEscape = '\\';        int searchForPos = 0;        int searchForEnd = searchForWildcard.length();        int searchInPos = 0;        int searchInEnd = searchIn.length();        while (searchForPos != searchForEnd) {            char wildstrChar = searchForWildcard.charAt(searchForPos);            while ((searchForWildcard.charAt(searchForPos) != wildcardMany) &&                    (wildstrChar != wildcardOne)) {                if ((searchForWildcard.charAt(searchForPos) == wildcardEscape) &&                        ((searchForPos + 1) != searchForEnd)) {                    searchForPos++;                }                if ((searchInPos == searchInEnd) ||                        (Character.toUpperCase(searchForWildcard.charAt(                                searchForPos++)) != Character.toUpperCase(                            searchIn.charAt(searchInPos++)))) {                    return WILD_COMPARE_MATCH_WITH_WILD; /* No match */                }                if (searchForPos == searchForEnd) {                    return ((searchInPos != searchInEnd)                    ? WILD_COMPARE_MATCH_WITH_WILD : WILD_COMPARE_MATCH_NO_WILD); /* Match if both are at end */                }                result = WILD_COMPARE_MATCH_WITH_WILD; /* Found an anchor char     */            }            if (searchForWildcard.charAt(searchForPos) == wildcardOne) {                do {                    if (searchInPos == searchInEnd) { /* Skip one char if possible */                        return (result);                    }                    searchInPos++;                } while ((++searchForPos < searchForEnd) &&                        (searchForWildcard.charAt(searchForPos) == wildcardOne));                if (searchForPos == searchForEnd) {                    break;                }            }            if (searchForWildcard.charAt(searchForPos) == wildcardMany) { /* Found w_many */                char cmp;                searchForPos++;                /* Remove any '%' and '_' from the wild search string */                for (; searchForPos != searchForEnd; searchForPos++) {                    if (searchForWildcard.charAt(searchForPos) == wildcardMany) {                        continue;                    }                    if (searchForWildcard.charAt(searchForPos) == wildcardOne) {                        if (searchInPos == searchInEnd) {                            return (WILD_COMPARE_NO_MATCH);                        }                        searchInPos++;                        continue;                    }                    break; /* Not a wild character */                }                if (searchForPos == searchForEnd) {                    return WILD_COMPARE_MATCH_NO_WILD; /* Ok if w_many is last */                }                if (searchInPos == searchInEnd) {                    return WILD_COMPARE_NO_MATCH;

⌨️ 快捷键说明

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