Java字符串处理:随机字符串,提取数字,身份证号码

2022/4/21 11:12:31

本文主要是介绍Java字符串处理:随机字符串,提取数字,身份证号码,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

一、随机字符串

 /**
     * 
     * @param count 随机字符串长度
     * @return
     */
    public static String getSecureRandomStr(int count){
        try {
            String[] texts = {"1","2","3","4","5","6","7","8","9","0",
                    "q","w","e","r","t","y","u","i","o","p",
                    "a","s","d","f","g","h","j","k","l",
                    "z","x","c","v","b","n","m",
                    "Q","W","E","R","T","Y","U","I","O","P",
                    "A","S","D","F","G","H","J","K","L",
                    "Z","X","C","V","B","N","M"};
            SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
            StringBuffer stringBuffer = new StringBuffer("");
            for(int i= 0;i<count;i++){
                stringBuffer.append(texts[random.nextInt(texts.length-1)]);
            }
            return stringBuffer.toString();
        }catch (Exception e){
            /* 假如异常,则调用伪随机数生成字符串 */
            return getRandomStr(count);
        }
    }

二、提取身份证号码

 /**
     * 过滤身份证号码
     * 通过对比  ASCII码 过滤指定字符串
     * */
    public static String getIdcard(String str){
        String str2="";
        if(str != null && !"".equals(str)){
            for(int i=0;i<str.length();i++){
                if((str.charAt(i)>=48 && str.charAt(i)<=57) || str.charAt(i)== 88 || str.charAt(i)== 120 ){
                    str2+=str.charAt(i);
                }
            }

        }
        return str2.toUpperCase();
    }

/**
     * 正则提取身份证号码
     * @return 提取到的身份证号码
     */
    public static String getIdCardPattern(String text){
        Pattern IdCard = Pattern.compile("\\d{17}[\\d|x|X]|\\d{15}");
        Matcher IdCardNumber = IdCard.matcher(text);//截取身份证号
        if(IdCardNumber.find()){
            String group = IdCardNumber.group();//得到身份证号
            return group;
        }else{
            return null;
        }
    }

三、提取数字

/**
     * 提取数字
     * 通过对比  ASCII码 提取数字
     * */
    public static String getInt(String str){
        String str2="";
        if(str != null && !"".equals(str)){
            for(int i=0;i<str.length();i++){
                if((str.charAt(i)>=48 && str.charAt(i)<=57)){
                    str2+=str.charAt(i);
                }
            }
        }
        return str2.toUpperCase();
    }
	
/**
     * 正则提取数字
     * @param text
     * @return
     */
    public static String getIntPattern(String text){
        Pattern IdCard = Pattern.compile("[^0-9]");
        Matcher IdCardNumber = IdCard.matcher(text);//截取身份证号
        if(IdCardNumber.find()){
            String group = IdCardNumber.replaceAll("").trim();//得到身份证号
           return group;
        }else{
            return null;
        }
    }

四、根据长度和数字获取编码

 /**
     * 根据数字获取固定长度的字符,作为编码时使用
     * 示例:getStringLength(0,6) 返回:000015
     * */
    public static String getStringLength(int length,int number){
        NumberFormat n = NumberFormat.getInstance();
        /* 这行的主要作用是去除千分位的逗号 */
        n.setGroupingUsed(false);
        n.setMinimumIntegerDigits(length);
        return n.format(number);
    }


这篇关于Java字符串处理:随机字符串,提取数字,身份证号码的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程