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

📄 stringutil.java

📁 OBPM是一个开源
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            return false;
        }

        try {
            DateUtil.parseTime(s);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * Checks whether the string a valid number fromat. Valid numbers include
     * hexadecimal marked with the "0x" qualifier, scientific notation and
     * numbers marked with a type qualifier (e.g. 123L).
     * Null and blank string will return false.
     * @param s The string to check
     * @return true if the string is a correctly formatted number, false otherwise.
     */
    public static boolean isNumber(String s) {
        if ((s == null) || (s.length() == 0))
            return false;

        char[] chars = s.toCharArray();
        int sz = chars.length;
        boolean hasExp = false;
        boolean hasDecPoint = false;
        boolean allowSigns = false;
        boolean foundDigit = false;
        int start = (chars[0] == '-') ? 1 : 0;

        if (sz > start + 1) {
            if (chars[start] == '0' && chars[start + 1] == 'x') {
                int i = start + 2;
                if (i == sz) {
                    return false;
                }
                for (; i < chars.length; i++) {
                    if ((chars[i] < '0' || chars[i] > '9')
                            && (chars[i] < 'a' || chars[i] > 'f')
                            && (chars[i] < 'A' || chars[i] > 'F')) {
                        return false;
                    }
                }
                return true;
            }
        }
        sz--;
        int i = start;
        while (i < sz || (i < sz + 1 && allowSigns && !foundDigit)) {
            if (chars[i] >= '0' && chars[i] <= '9') {
                foundDigit = true;
                allowSigns = false;

            } else if (chars[i] == '.') {
                if (hasDecPoint || hasExp) {
                    return false;
                }
                hasDecPoint = true;
            } else if (chars[i] == 'e' || chars[i] == 'E') {
                if (hasExp) {
                    return false;
                }
                if (!foundDigit) {
                    return false;
                }
                hasExp = true;
                allowSigns = true;
            } else if (chars[i] == '+' || chars[i] == '-') {
                if (!allowSigns) {
                    return false;
                }
                allowSigns = false;
                foundDigit = false;
            } else {
                return false;
            }
            i++;
        }
        if (i < chars.length) {
            if (chars[i] >= '0' && chars[i] <= '9') {
                return true;
            }
            if (chars[i] == 'e' || chars[i] == 'E') {
                return false;
            }
            if (!allowSigns
                    && (chars[i] == 'd' || chars[i] == 'D' || chars[i] == 'f' || chars[i] == 'F')) {
                return foundDigit;
            }
            if (chars[i] == 'l' || chars[i] == 'L') {
                return foundDigit && !hasExp;
            }
        }

        return !allowSigns && foundDigit;
    }

    /**
     * Unite the string arrary into one string.
     * @param arr The string arrary
     * @param sp The split tag.
     * @return The string after unite.
     */
    public static String unite(String[] arr, String sp) {
        if (arr == null)
            return null;

        if (arr.length == 0)
            return "";

        int i;
        
        StringBuffer buff = new StringBuffer();        
        for (i = 0; i < arr.length - 1; i++) 
            buff = buff.append(arr[i]).append(sp);

        buff = buff.append(arr[i]);

        return buff.toString();
    }

    /**
     * Unite the string arrary into one string with ";".
     * @param arr The string arrary
     * @returnThe string after unite.
     */
    public static String unite(String[] arr) {
        return unite(arr, ";");
    }

    /**
     * Encode the url to UTF format.
     * @param s The target URL
     * @param sp the sperate taf.
     * @return The url after encode.
     */
    public static String chineseURLToUTF(String s, char sp) {
        String filename = (new File(s)).getName();
        String path = (new File(s)).getParent();

        path = path.replace('\\', sp);
        try {
            filename = java.net.URLEncoder.encode(filename, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        path = path + "/" + filename;
        return path;
    }
    /**
     * Encode the string to GB2312
     * @param s The Target string
     * @return The string after encode.
     */
    public static String toGB2312(String s) throws Exception{
        if (s != null && s.length() > 0) {
            byte[] byteTmp = s.getBytes("ISO8859_1");
            s = new String(byteTmp, "GB2312");
        }
        return s;
    }

    /**
     * Encode the string to Iso 8859.
     * @param s The Target string
     * @return The string after encode.
     */
    public static String to8859(String s) throws Exception {
        if (s != null && s.length() > 0) {
            byte[] byteTmp = s.getBytes("GB2312");
            s = new String(byteTmp, "8859_1");
        }
        return s;
    }

    /**
     * Encode the string in html format.
     * @param s The string.
     * @return The string in html format.
     */
    public static String getHTMLString(String s) {
        if (s == null)
            return ("");
        if (s.equals(""))
            return ("");

        StringBuffer buf = new StringBuffer();
        char ch1 = '\n';
        char ch2 = '\n';

        for (int i = 0; i < s.length(); i++) {
            ch1 = s.charAt(i);

            if ((ch1 == ' ') && ((i + 1) < s.length())) {
                ch2 = s.charAt(i + 1);
                if (ch2 == ' ') {
                    buf.append("\u00A1\u00A1");
                    i++;
                } else {
                    buf.append(ch1);
                }
            } else if (ch1 == '\n') {
                buf.append("<br>");
            } else if (ch1 == '\t') {
                buf.append("\u00A1\u00A1\u00A1\u00A1");
            } else {
                buf.append(ch1);
            }
        }

        return buf.toString();
    }

    /**
     * Encode the string in html format.
     * @param t The target text
     * @return the string after encode.
     */
    public static String encodeHTML(String t) {
        t = t.replaceAll("&", "@amp;");
        t = t.replaceAll("\"", "@quot;");
        t = t.replaceAll("<", "@lt;");
        t = t.replaceAll(">", "@gt;");
        t = t.replaceAll("'", "@#146;");
        t = t.replaceAll(" ", "@nbsp;");
        t = t.replaceAll("#","%23");
        return t;
    }
    /**
     * Decode the html format.
     * @param t The target format.
     * @return The string after decode.
     */
    public static String dencodeHTML(String t) {
        t = t.replaceAll("&amp;", "&");
        t = t.replaceAll("&quot;", "\"");
        t = t.replaceAll("&lt;", "<");
        t = t.replaceAll("&gt;", ">");
        t = t.replaceAll("&#146;", "'");
        t = t.replaceAll("&nbsp;", " ");
        t = t.replaceAll("&#13;", "\n");
        t = t.replaceAll("&#10;", "\n");
        
        t = t.replaceAll("@amp;", "&");
        t = t.replaceAll("@quot;", "\"");
        t = t.replaceAll("@lt;", "<");
        t = t.replaceAll("@gt;", ">");
        t = t.replaceAll("@#146;", "'");
        t = t.replaceAll("@nbsp;", " ");
        t = t.replaceAll("&#9;", " ");
        t = t.replaceAll("%23", "#");
        return t;
    }

    /**
     * Check whether the speical string include the chinese word.
     * @param str The speical string 
     * @return true for include , false otherwise.
     * @throws Exception
     */
    public static boolean haveChinesewords(String str) throws Exception{
        return !toGB2312(str).equals(str);
    }

    /**
     * Get the same sub string between two string.
     * @param s1 The first string.
     * @param s2 The second string.
     * @return The same sub string 
     */
    public static String getSameString(String s1, String s2) {
        String s = "";
        if (s1 == null || s1.trim().length() <= 0 || s2 == null
                || s2.trim().length() <= 0)
            return s;

        int len = s1.length() > s2.length() ? s2.length() : s1.length();
        System.out.println("len->" + len);
        char[] c1 = s1.toCharArray();
        char[] c2 = s2.toCharArray();
        for (int i = 0; i < len; i++) {
            if (c1[i] == c2[i]) {
                s += c1[i];
                continue;
            } else {
                break;
            }
        }
        return s;
    }
    
	public static String getFixLengthString(String str, int length) throws Exception {   //***************
		if(str==null||str.trim().length()<0) return getBlankString(length);
		String reStr = "";
		//去掉回车
		str = str.replaceAll("\r", "");
		str = str.replaceAll("\n", "");
		str = new String(str.getBytes(), "8859_1");
		if (str.length() >= length) {
			reStr = str.substring(0, length);
		} else {
			reStr = str + getBlankString(length - str.length());
			//System.out.println("length:"+length+" restr's
			// length:"+reStr.length());
		}
		byte[] bytesStr = reStr.getBytes("8859_1");
		reStr = new String(bytesStr, "gb2312");
		return reStr;
	}

	public static String getBlankString(int count) {
		String str = "";
		for (int i = 0; i < count; i++) {
			str = str + " ";
		}
		return str;
	}
}

⌨️ 快捷键说明

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