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

📄 stringutility.java

📁 采用JAVA开发
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
            '0',
            '`',
            '~',
            '!',
            '@',
            '#',
            '$',
            '%',
            '^',
            '&',
            '*',
            '(',
            ')',
            '-',
            '_',
            '=',
            '+',
            '|',
            '\\',
            '[',
            ']',
            '{',
            '}',
            '\'',
            '\"',
            ';',
            ':',
            ',',
            '.',
            '<',
            '>',
            '/',
            '?' };

    /**
     * 获得截取到指定长度后的字符串,多出部分用指定字符串代替。
     *
     * @param str 需要限定长度的字符串
     * @param length 截取位数,汉字算两位
     * @param more 有更多情况的替换字符串
     * @return 截取后的字符串
     */
    public static String getMoreString(String str, int length, String more) {
        if (str == null || str.equals(""))
            return "";
        else {
            int len = str.length();
            int curLength = 0;
            StringBuffer buf = new StringBuffer();
            boolean isSingleChar;
            boolean hasMore = false;
            for (int i = 0; i < len; i++) {
                isSingleChar = false;
                if (curLength < length) {
                    for (int j = 0; j < ascchars.length; j++) {
                        if (str.charAt(i) == ascchars[j]) {
                            buf.append(str.charAt(i));
                            curLength++;
                            isSingleChar = true;
                            break;
                        }
                    }
                    if (!isSingleChar) {
                        buf.append(str.charAt(i));
                        curLength += 2;
                    }
                }
                else
                    hasMore = true;
            }
            if (more == null) {
                more = "...";
            }
            if (hasMore)
                return buf.append(more).toString();
            else
                return buf.toString();
        }
    }

    public static String getNextLevel(String maxLevel, int level) {
        String ParentLevel = maxLevel.substring(0, maxLevel.length() - 3);
        String curr = maxLevel.substring(maxLevel.length() - 3, maxLevel.length());
        int iNext = Integer.parseInt(curr) + 1;
        return ParentLevel + leftPad(iNext, level, '0');
    }

    /**
     * 将数字转化为字符串,并格式化为指定的长度,不够位数的前面补指定字符
     * Input: 1, 5, '0'
     * Output: "00001"
     *
     * @return java.lang.String
     * @param value - 要转换的数字
     * @param number - 要补齐的位数
     * @param c char - 补充的字符
     */
    public static String leftPad(int value, int number, char c) {
        String s = "";
        String temp = (new Integer(value)).toString();
        if (temp.length() <= number) {
            for (int i = 0; i < number; i++) {
                s += c;
            }
            s = s.substring(0, number - temp.length()) + temp;
        }
        else {
            System.out.println("Error: " + number + "'s length is " + temp.length() + ", it's bigger than " + number + ".");
        }
        return s;
    }

    /**
     * 随机生成一个指定长度的整数字符串
     *
     * @return java.lang.String
     * @param int num - 要生成的字符串长度
     */
    public static String random(int num) {
        String ret = "";
        for (int i = 0; i < num; i++) {
            int randomInt = (int) ((java.lang.Math.random()) * 10);
            ret = ret.concat(Integer.toString(randomInt));
        }
        return ret;
    }

    /**
     * 从字符串中得到文件名
     *
     * @return java.lang.String
     * @param src java.lang.String
     */
    public static String getFileName(String str) {
        if (str == null)
            return "";
        int idx = str.lastIndexOf("/") + 1;
        if (idx == 0)
            return "";
        return str.substring(idx);
    }

    /**
     * 从字符串中得到文件路径
     *
     * @return java.lang.String
     * @param src java.lang.String
     */
    public static String getFileDir(String str) {
        if (str == null)
            return "";
        int idx = str.lastIndexOf("/");
        if (idx == 0)
            return "";
        return str.substring(0, idx);
    }
    /**
     * 把分割的字符串合成数组
     * @param src
     * @param delim
     * @return
     */
    public static String[] split(String src, String delim) {
        if (src == null || delim == null)
            return null;
        StringTokenizer st = new StringTokenizer(src, delim);
        Vector vct = new Vector();
        for (; st.hasMoreTokens(); vct.add(st.nextToken()));
        Object tks[] = vct.toArray();
        String rt[] = new String[vct.size()];
        System.arraycopy(((Object) (tks)), 0, rt, 0, vct.size());
        return rt;
    }
    
    /**
     * 判断给定字符串是否为空(add in 2003.11.20@author windy)
     *
     * @param str 需要检查的字符串
     * @return 如果为null或"",则返回true,否则为false
     */
    public static boolean isNullorBlank(final String str){
        boolean isNorB = false;
        if(null == str || 0 >= str.length()){
            isNorB = true;
        }
        return isNorB;
    }//end isNullorBlank()
    /**
     * convert string to int(add in 2003.11.20@author windy)
     * @param String StringNum
     * @return Int;if it can be converted retrun intNum,or return -1
     */
    public static int toInt(String StringNum){
        try
        {
            return Integer.parseInt(StringNum);
        }
        catch(Exception e)
        {return -1;}
    }//end toInt(String)

    /**
     * 查看源数据中是否包含后面的数据(add in 2003.11.21@author windy)
     * @param String source
     * @param String pattern
     * @return boolean;if contains retrun true,or return false
     */    
    public static boolean contains(String source, String pattern)
    {
        if(source == null || pattern == null)
            return false;
        source = "," + source.trim() + ",";
        pattern = "," + pattern.trim() + ",";
        return source.indexOf(pattern) != -1;
    }//eof contains(String,String)
    
    /**
     * 将给定的字符串转换为中文GBK编码的字符串;若为NULL,转换成空 ("")(add in 2003.11.21@author windy)
     *
     * @param str 输入字符串
     * @return 经GBK编码后的字符串,如果有异常,则返回原编码字符串
     */
    public static String toChinese(final String str){
        if(isNullorBlank(str)){
            return "";
        }
        String rn = str;
        try{
            rn = new String(str.getBytes("ISO8859_1"), "GBK");
        } catch(Exception e){
            //log...
        }
        return rn.trim();
    }//end toGb2312(String)

    /**
     * 将给定的中文GBK字符串转换为ISO编码的字符串;若为NULL,转换成空 ("")(add in 2003.11.21@author windy)
     *
     * @param str 输入字符串
     * @return 经GBK编码后的字符串,如果有异常,则返回原编码字符串
     */
    public static String toIso(final String str){
        if(isNullorBlank(str)){
            return "";
        }
        String rn = str;
        try{
            rn = new String(str.getBytes("GBK"), "ISO8859_1");
        } catch(Exception e){
            //log...
        }
        return rn.trim();
    }//end toIso(String)
    
    /**
     * 处理字符串中的单引号
     * 一个单引号变为两个单引号
     *
     * @return java.lang.String
     * @param oldValue java.lang.String
     */
    public static String processSingleQuotes(String oldValue) {
        // Process single quotes
        String newValue = new String();
        if (oldValue != null) {
            char c;
            for (int i = 0; i < oldValue.length(); i++) {
                c = oldValue.charAt(i);
                if (c == '\'') {
                    newValue += c;
                }
                newValue += c;
            }
        }
        return newValue;
    }
 

//    public static void main(String args[]) {
//        String str = "我是abc啊hehe";
//        System.out.println("getMoreString( \"" + str + "\", 7, \"...\" ) = " + getMoreString(str, 7, "..."));
//        System.out.println("getMoreString( \"" + str + "\", 8, \"...\" ) = " + getMoreString(str, 8, "..."));
//        System.out.println("getMoreString( \"" + str + "\", 9, \"...\" ) = " + getMoreString(str, 9, "..."));
//        System.out.println("getMoreString( \"" + str + "\", 10, \"...\" ) = " + getMoreString(str, 10, "..."));
//        System.out.println("getMoreString( \"" + str + "\", 50, \"...\" ) = " + getMoreString(str, 50, "..."));
//
//        //Test random()
//        System.out.println("the method random(10)=" + StringUtility.random(10));
//    }
}

⌨️ 快捷键说明

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