正则表达式的简单运用

    /**
     * @return 请求批次编号(yyyyMMddhhmmss+6位随机数)
     */
    public static String  getRandomNum(){
        String  code="";
        Random random = new Random();
        Timestamp timestamp = new Timestamp(System.currentTimeMillis());
        String temp= timestamp.toString().replaceAll("[- :]", "").substring(0, 14);
        for (int i = 0; i < 6; i++) {
            code += random.nextInt(9);
        }
        return temp+code;
    }
    
    /**
     * 处理格式
     *  ""  "3045056,3045057"  "3045056,3045057,3045058"
     * @param str
     * @return
     */
    public String  putGother(String str){
        str = str.replaceAll(",{2,}", ","); //替换多个,
        if (str.indexOf(",")==0) {
            str = str.substring(1, str.length()); 
        }
        if (str.lastIndexOf(",")>0) {
            str = str.substring(0, str.length()-1); 
        }
        return str;
    }
原文地址:https://www.cnblogs.com/lxh520/p/8418350.html