当前位置: 代码迷 >> 综合 >> 字符串转化为默认规的长度
  详细解决方案

字符串转化为默认规的长度

热度:14   发布时间:2024-01-16 15:15:25.0

   

需要转化的字符串user_no,转化规则为以H开头后跟default_length位数字的字符串,如果少于default_length位,则在数字长加0补长为default_length位

    private static final int default_length = 8;

    public static String transletString(String user_no)
    {
        int userNolen = 0;
        String user_code = "";
        String temp_user_code = "";
       
        userNolen = user_no.length();
        if(userNolen < default_length)
        {
            int temp_length = default_length - userNolen;
            for(int i=0;i<temp_length;i++)
            {
                temp_user_code += "0";
            }
            user_code = "H" + temp_user_code + user_no;
        }
        else
            user_code = "H" + user_no;
       
        return user_code;
    }

  相关解决方案