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

📄 stringutil.java

📁 一个webwork+spring+ibatis的小例子
💻 JAVA
📖 第 1 页 / 共 2 页
字号:

    }

    /** 判断是否为整数 */
    public static boolean isInteger(String input) {
        boolean result = false;
        try {
            Integer.parseInt(input);
            result = true;
        } catch (NumberFormatException e) {
            result = false;
        }
        return result;
    }

    /** 由字符串前后用逗号","隔开的字符串得到整形数组 */
    public static int[] getIntAryByString(String input) {
        int[] result = new int[0];

        if (input != null && !input.equals("") && !input.equals(",")) {

            String[] temp = input.split(",");
            int k = 0;

            ArrayList tmpList = new ArrayList();

            if (temp != null)
                k = temp.length;

            for (int i = 0; i < k; i++) {
                if (isInteger(temp[i])) {
                    tmpList.add(temp[i]);
                }
            }

            if (tmpList != null) {
                int len = tmpList.size();

                result = new int[len];

                for (int i = 0; i < len; i++) {

                    result[i] = Integer.parseInt((String) tmpList.get(i));

                }

            }
        }

        return result;

    }

    /** 得到部分字符串 */
    public static String substring(String sourceStr, int start, int end) {
        String result = "";
        int strLen = 0;
        if (sourceStr != null) {
            strLen = sourceStr.length();
        }

        start = (start > strLen) ? strLen : start;
        end = (strLen > end) ? end : strLen;
        if (sourceStr != null) {
            result = sourceStr.substring(start, end);
        }
        return result;
    }
    
    /** 得到部分字符串 */
    public static int substring(int sourceNum, int start, int end) {
        String result = "";
        int strLen = 0;
        String sourceStr=sourceNum+"";
       
        if (sourceStr != null) {
            strLen = sourceStr.length();
        }

        start = (start > strLen) ? strLen : start;
        end = (strLen > end) ? end : strLen;
        if (sourceStr != null) 
        {
            result = sourceStr.substring(start, end);
        }
        return (new Integer(result)).intValue() ;
    }  
    
    public static int ObjectToInt(Object str)
    {
        try
        {
            String strTmp = "";
            if(str != null)
                strTmp = String.valueOf(str);
            return Integer.parseInt(strTmp);
        }
        catch(NumberFormatException e)
        {
            return 0;
        }
    }
    
    public static String getOKDirName(String strFileName) {
        String result = "";
        result = replace(strFileName, "<", "");
        result = replace(result, ">", "");
        result = replace(result, "*", "");
        result = replace(result, ":", "");
        result = replace(result, "?", "");
        result = replace(result, "\\", "");
        result = replace(result, "/", "");
        result = replace(result, "|", "");
        result = replace(result, "\"", "");
        return result;

    }

    public static String replaceDeCode(String str) {
        if (str != null) {
            str = str.replaceAll("\n", "<br>");
            str = str.replaceAll(" ", "&nbsp;");
        }
        return str;
    }

    public static String replaceEnCode(String str) {
        if (str != null) {
            str = str.replaceAll("<br>", "\n");
            str = str.replaceAll("&nbsp;", " ");
        }
        return str;
    }

    /** 得到固定位数的值如1变为01成两位 */
    public static String getZeroFill(String str, int intLen) {
        String returnVal;
        str = str.trim();
        returnVal = str;
        int thisLength = str.length();
        for (int j = thisLength; j < intLen; j++)
            returnVal = "0" + returnVal;

        return returnVal;
    }

    /** 得到指定编码格式的字符长度 */
    public static int getStrLength(String txt, String charset) {
        try {
            return txt.getBytes(charset).length;
        } catch (UnsupportedEncodingException ex) {
            return txt.length();
        }
    }

    /** 得到字符串的 */
    public static String encodeURL(String url, String charset) {
        if (url != null && url.length() > 0) {
            try {
                return URLEncoder.encode(url, charset);
            } catch (UnsupportedEncodingException ex) {
                return url;
            }
        }
        return url;
    }

    public static String replaceSpecChar(String str) {
        str = replace(str, "\\", "\\\\");
        str = replace(str, "\'", "\\'");
        return str;
    }

    public static boolean isEmpty(String str) {
        if (str == null)
            return true;
        else if (str.trim().equals(""))
            return true;
        return false;
    }

    public static Object htmlSpecChars(Object str) 
    {
        
        Object rtn = str;
        if((rtn != null)&&(rtn.getClass().getName().equals(String.class.getName())))
        {
            String strTmp = (String)str;
            if (!strTmp.equals(""))
            {
                strTmp = strTmp.replaceAll("&", "&amp;");
                strTmp = strTmp.replaceAll("\"", "&quot;");
                strTmp = strTmp.replaceAll("'", "&#039;");
                strTmp = strTmp.replaceAll("<", "&lt;");
                strTmp = strTmp.replaceAll(">", "&gt;");
                rtn = strTmp;
            }
        }
        return rtn;
    }
    
    public static Object nl2br(Object str) 
    {
        
        Object rtn = str;
        if((rtn != null)&&(rtn.getClass().getName().equals(String.class.getName())))
        {
            String strTmp = (String)str;
            if (!strTmp.equals(""))
            {
                strTmp = strTmp.replaceAll("\n", "<br/>");
                strTmp = strTmp.replaceAll(" ", "&nbsp;");
                rtn = strTmp;
            }
        }
        return rtn;
    }


    public static String replaceStr(String str, String separator,
            String replacestr, int maxLine, String lastStr) {
        if (isEmpty(str))
            return "";
        String[] strList = str.split(separator);
        StringBuffer sb = new StringBuffer();
        int blankLine = 0;
        for (int i = 0; i < strList.length; i++) {
            if (maxLine != 0 && i-blankLine > maxLine-1) {
                sb.append(lastStr);
                break;
            }
            if (!isEmpty(strList[i])) {
                sb.append(strList[i]);
                if (i + 1 < strList.length)
                    sb.append(replacestr);
            }
            else
            {
                blankLine++;
            }
        }
        return sb.toString();
    }

    /**
     * @author 2005-3-26 12:03:07 edusaj
     * @param inputList
     *            输入的List值
     * @param keyIn
     *            输入的键值数据
     * @param finedValue
     *            输入的查找对应值数据,个数与keyIn的个数同
     * @param keyOut
     *            返回Map中的键值对
     * @return 由输入的条件等到一组满足的条件的Map
     */
    public static Map getFindMapFromListByKey(List inputList, String[] keyIn,
            String[] finedValue, String[] keyOut) {
        Map rtnMap = null;
        if (inputList != null) {
            int k = inputList.size();
            Map tempMap = null;

            for (int i = 0; i < k; i++) {
                tempMap = (HashMap) inputList.get(i);

                if (tempMap != null) {

                    boolean existsFind = false;
                    boolean checkExistsFind = true;

                    /** 输入的参数与会值都满足 */
                    if (keyIn != null && finedValue != null
                            && keyIn.length == finedValue.length) {

                        int m = keyIn.length;

                        for (int n = 0; n < m; n++) {

                            checkExistsFind = checkExistsFind
                            && tempMap.containsKey(keyIn[n])
                                    && (tempMap.get(keyIn[n]).toString())
                                            .equals(finedValue[n]);
                        }

                    }

                    existsFind = checkExistsFind;

                    if (existsFind) {                        

                        rtnMap = new HashMap();
                        if (keyOut != null) {
                            int s = keyOut.length;
                            rtnMap.put(keyIn, finedValue);
                            for (int j = 0; j < s; j++) {
                                rtnMap.put(keyOut[j], tempMap.get(keyOut[j]));
                            }
                        }
                        /** 如果存在,则退出 */
                        break;
                    }

                }
            }
        }
        return rtnMap;
    }

    /**
     * @author 2005-3-26 12:03:43 edusaj
     * @param inputList
     *            输入的List值
     * @param keyIn
     *            输入的键值
     * @return 返回由键值得到的值以逗号","隔开的字符串
     */
    public static String getSplitOutByList(List inputList, Object keyIn) {
        String rtn = "0";

        if (inputList != null) {
            StringBuffer out = new StringBuffer();

            int k = inputList.size();
            Map tempMap = null;
            for (int i = 0; i < k; i++) {
                tempMap = (HashMap) inputList.get(i);
                if (tempMap != null && tempMap.containsKey(keyIn)) {
                    out.append("," + tempMap.get(keyIn));
                }
            }

            if (out != null) {
                rtn += out.toString();
            }
        }

        return rtn;
    }
    
}

⌨️ 快捷键说明

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