java convert工具类

这是一个Java工具类,包含多种字符串处理方法,如去除右边空格、前后增加指定字符、格式化数字等。例如,rightTrim方法用于移除字符串末尾的空格,increaseChar方法可以在字符串后增加指定数量的特定字符。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

工具类,给希望使用的人

public class Convert
{
    // 去除右边的空格
    public static String rightTrim(String line)
    {
        int len = line.length();
        if (len == 0) return "";
        
        int st = 0;
        for (int i = len - 1; i >= 0; i--)
        {
            if (line.charAt(i) != ' ') {
                st = i;
                break;
            }
        }

        return line.substring(0, st + 1);
    }

    //增加num个空格  后缀
    public static String increaseChar(String str, int num) {
        return increaseChar(str, ' ', num);
    }

    /**
     *
    * @Title: increaseChar
    * @Description: TODO(这里用一句话描述这个方法的作用)
    * @param str    原始字符串
    * @param c    后缀字符
    * @param num    字符数量
    * @return String    返回类型
     */
    public static String increaseChar(String str, char c, int num)
    {
        int limit;
        if (num <= 0)
            limit = 16;
        else
            limit = num;
        
        int len = str.length();
        for (int i = len; i < limit; i++)
            str = str + c;
        
        if (str.length() > limit)
            return str.substring(str.length() - limit);
        
        return str;
    }

    //增加num个前置空格
    public static String increaseCharForward(String str, int num) {
        return increaseCharForward(str, ' ', num);
    }
    
    /**
     *
    * @Title: increaseCharForward
    * @Description: 增加前置字符
    * @param str         原始字符串
    * @param c        前置字符
    * @param num    数量
    * @return String    返回类型
     */
    public static String increaseCharForward(String str, char c, int num)
    {
        int limit;
        if (num <= 0)
            limit = 16;
        else
            limit = num;
        
        int len = str.length();
        for (int i = len; i < limit; i++)
            str = c + str;
        
        if (str.length() > limit)
            return str.substring(str.length() - limit);
        
        return str;
    }

    //增加lim个前置0
    public static String increaseInt(int num, int lim)
    {
        String str = "";
        int limit;

        if (lim <= 0)
            limit = 4;
        else
            limit = lim;

        String a = String.valueOf(num);
        int len = a.length();
        for (int i = 0; i < limit - len; i++)
            str = str + "0";

        str = str + num;
        if (str.length() > limit)
            return str.substring(str.length() - limit);
        
        return str;
    }

    public static String increaseDou(double num, int lim)
    {
        String str = "";
        int limit;

        if (lim <= 0)
            limit = 16;
        else
            limit = lim;

        String a = String.valueOf(num);
        int len = a.length();
        for (int i = 0; i < limit - len; i++)
            str = str + "0";

        str = str + num;
        if (str.length() > limit)
            return str.substring(str.length() - limit);
        
        return str;
    }

    public static String increaseLong(long num, int lim)
    {
        String str = "";
        int limit;
        if (lim <= 0)
            limit = 8;
        else
            limit = lim;

        String a = String.valueOf(num);
        int len = a.length();
        for (int i = 0; i < limit - len; i++)
            str = str + "0";

        str = str + num;
        if (str.length() > limit)
            return str.substring(str.length() - limit);
        
        return str;
    }

    public static String convertPasswd(String passwd)
    {
        String line = "";
        int len = passwd.length();
        for (int i = 2; i < len; i += 2)
        {
            line = line + "," + Integer.valueOf(passwd.substring(i, i + 2), 16).toString();
        }

        return line.substring(1);
    }

    public static String appendStringSize(String orgLine, String addLine, int startIndex, int length, int orgSize) {
        return appendStringSize(orgLine, addLine, startIndex, length, orgSize, 0);
    }

    //
    public static String appendStringSize(String orgLine, String addLine,
            int startIndex, int length, int orgSize, int alignment)
    {
        String newLine = orgLine;
        int orgLength = countLength(orgLine);
        if (orgLength > orgSize) {
            newLine = newSubString(orgLine, 0, orgSize);
        }
        else
        {
            for (int i = orgLength; i < orgSize; i++)
                newLine = newLine + " ";
        }

        if (addLine != null)
        {
            if (countLength(addLine) >= length)
            {
                addLine = newSubString(addLine, 0, length);
            }
            else
            {
                int sub = 0;
                switch (alignment)
                {
                case 0:
                    sub = 0;
                    break;
                    
                case 1:
                    sub = length - countLength(addLine);
                    break;
                    
                case 2:
                    sub = (length - countLength(addLine)) / 2;
                    break;
                    
                }

                for (int i = countLength(addLine); i < length; i++)
                {
                    if (sub > 0)
                    {
                        addLine = " " + addLine;
                        sub--;
                    }
                    else
                    {
                        addLine = addLine + " ";
                    }
                }
            }

            String head = newSubString(newLine, 0, startIndex);
            String end = "";
            try
            {
                end = newSubString(newLine, startIndex + length, orgSize);
            }
            catch (Exception er)
            {
                er.printStackTrace();
            }

            newLine = head + addLine + end;
            if (countLength(newLine) > orgSize)
            {
                newLine = newSubString(newLine, 0, orgSize);
            }
        }

        return newLine;
    }

    public static String newSubString(String line, int start) {
        return newSubString(line, start, countLength(line));
    }

    // 将打不出来的部分赋值给商品名称换行打印项
    public static String newSubString(String line, int start, int end)
    {
        if (start >= end)
            return "";

        if (countLength(line) <= end - start)
            return line;

        boolean flag = false;
        int startindex = 0;
        int endindex = 0;
        int length = 0;

        for (int i = 0; i < line.length(); i++)
        {
            char c = line.charAt(i);
            if (c > 127)  // \177   //ASCII码总共128个    \000 ~ \177 (即0-127)
                length += 2;
            else
                length++;
            
            if (length >= end + 1) {
                endindex = i;
                break;
            }

            if ((length >= start + 1) && (!flag)) {
                flag = true;
                startindex = i;
            }
        }

        if (startindex > endindex) {
            return line.substring(startindex);
        }

        return line.substring(startindex, endindex);

    }

    
    /**
     * 统计字符串长度
     * @param line
     * @return
     * 只要在0~127中的ASCII中当成一个,大于127当成中文2个长度
     */
    public static int countLength(String line)
    {
        int length = 0;
        for (int i = 0; i < line.length(); i++)
        {
            char c = line.charAt(i);
            // if (c > '') {
            if (c > 127)     //'177'  中文字符算2个长度
                length += 2;
            else
                length++;
            
        }

        return length;
    }

    //判断字符串是否为空
    public static boolean isNull(Object obj) {
        return obj == null || obj.toString().trim() == "";
    }

    //判断对象是数字
    public static boolean isNumber(Object obj) {
        try
        {
            Double.parseDouble(obj.toString());
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
        
    }

    //
    public static boolean isShort(Object obj) {
        try
        {
            Short.parseShort(obj.toString());
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
        
    }

    public static boolean isInt(Object obj) {
        try
        {
            Integer.parseInt(obj.toString());
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
        
    }

    public static boolean isLong(Object obj) {
        try
        {
            Long.parseLong(obj.toString());
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
        
    }

    //是否为字母
    public static boolean isLetter(char c)
    {
        if (((c >= 'a') && (c <= 'z')) || ((c >= 'A') && (c <= 'Z')))
        {
            return true;
        }
        return false;
    }

    //
    public static boolean isDouble(Object obj) {
        try
        {
            Double.parseDouble(obj.toString());
            return true;
        }
        catch (Exception e)
        {
            return false;
        }
        
    }

    public static double toDouble(Object obj)
    {
        try
        {
            if (obj == null) return 0.0D;
            return Double.parseDouble(obj.toString());
        }
        catch (Exception e)
        {
            return 0.0D;
        }
        
    }

    public static int toInt(char c) {
        try
        {
            return Character.getNumericValue(c);
        }
        catch (Exception e)
        {
            return 0;
        }

    }

    public static short toShort(Object obj) {
        try
        {
            return (short) (int) toDouble(obj);
        }
        catch (Exception e)
        {
            return 0;
        }
        
    }

    public static int toInt(Object obj) {
        try
        {
            return (int) toDouble(obj);
        }
        catch (Exception e)
        {
            return 0;
        }
        
    }

    public static long toLong(Object obj) {
        try
        {
            return (long) toDouble(obj);
        }
        catch (Exception e)
        {
            return 0L;
        }
        
    }

    
    
    /**
     * 取得字符串text中的代码  
     *  以-  取 开头到 -位置的字符
     * 以[]  取中间的字符串
     *
     */
    public static String codeInString(String text, char c)
    {
        int pos = 0;
        if (c == '-')
        {
            pos = text.indexOf(c);
            if (pos < 0)
                return "";
            return text.substring(0, pos + 1);
        }
        if ((c == '[') || (c == ']'))
        {
            pos = text.indexOf('[');
            if (pos < 0)
                return "";
            int len = text.indexOf(']');
            if (len < 0)
                return "";
            return text.substring(pos + 1, len - pos);
        }
        return text;
    }
    
    /**
     * add by ssj 2018.11.23
     * 判断字符串是否为空
     * @param str
     * @return
     */
    public static boolean stringIsEmpty(String str) {
        if (str != null  && !"".equals(str.trim())) {
            return false;
        }
        return true;
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值