Java 实用小方法

 public static boolean isValid(String val) {
        if (val == null || "".equals(val.trim())) {
            return false;
        }
        return true;
    }

 

    //将数字转换成英文,

  public static final String[] enNum = { //基本数词表 
        "zero", "one", "tow", "three", "four", "five",
        "six", "seven", "eight", "nine", "ten",
        "eleven", "twelve", "thirteen", "fourteen", "fifteen",
        "sixteen", "seventeen", "eighteen", "nineteen", "twenty",
        "", "", "", "", "", "", "", "", "", "thirty",
        "", "", "", "", "", "", "", "", "", "fourty",
        "", "", "", "", "", "", "", "", "", "fifty",
        "", "", "", "", "", "", "", "", "", "sixty",
        "", "", "", "", "", "", "", "", "", "seventy",
        "", "", "", "", "", "", "", "", "", "eighty",
        "", "", "", "", "", "", "", "", "", "ninety"
    };
    public static final String[] enUnit = {"hundred", "thousand", "million", "billion", "trillion", "quintillion"}; //单位表  

    public static String NumberToEngListStr(String num) { //数字字符串参数  
        //判断字符串是否为数字 
        if (!num.matches("\\d+")) {
            return String.format("%s is not number", num);
        }
        num = num.replaceAll("^[0]*([1-9]*)", "$1"); //把字符串前面的0去掉  
        if (num.length() == 0) { //如果长度为0,则原串都是0    
            return enNum[0];
        } else if (num.length() > 9) {  //如果大于9,即大于999999999,题目限制条件 
            return "too big";
        }           //按3位分割分组    
        int count = (num.length() % 3 == 0) ? num.length() / 3 : num.length() / 3 + 1;
        if (count > enUnit.length) {
            return "too big";
        } //判断组单位是否超过,
        //可以根据需求适当追加enUnit     
        String[] group = new String[count];
        for (int i = num.length(), j = group.length - 1; i > 0; i -= 3) {
            group[j--] = num.substring(Math.max(i - 3, 0), i);
        }
        StringBuilder buf = new StringBuilder(); //结果保存  
        for (int i = 0; i < count; i++) { //遍历分割的组    
            int v = Integer.valueOf(group[i]);
            if (v >= 100) { //因为按3位分割,所以这里不会有超过999的数  
                buf.append(enNum[v / 100]).append(" ").append(enUnit[0]).append(" ");
                v = v % 100; //获取百位,并得到百位以后的数     
                if (v != 0) {
                    buf.append("and ");
                } //如果百位后的数不为0,则追加and   
            }
            if (v != 0) {
                if (v < 20 || v % 10 == 0) { //如果小于20或10的整数倍,直接取基本数词表的单词      
                    buf.append(enNum[v]).append(" ");
                } else { //否则取10位数词,再取个位数词        
                    buf.append(enNum[v - v % 10]).append(" ");
                    buf.append(enNum[v % 10]).append(" ");
                }
                if (i != count - 1) { //百位以上的组追加相应的单位   
                    buf.append(enUnit[count - 1 - i]).append(" ");
                }
            }
        }
        return buf.toString().trim(); //返回值
    }

//获取字符串中的非中文字符

    public static String findNotChinese(String str) {
        Pattern p;
        Matcher m;
        String temp = null;
        String regEx = "[\\u4e00-\\u9fa5][\\u4e00-\\u9fa5]*";
        p = Pattern.compile(regEx);
        m = p.matcher(str);
        String strb = str;
        while (m.find()) {
            temp = m.group();
            strb = strb.replace(temp, "");
        }
        return strb;
    }

 

//判断是否为数字

public static boolean isNumeric(String str) {
        Pattern pattern = Pattern.compile("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
        return pattern.matcher(str).matches();
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值