随机生成指定长度的字符串+校验

最近项目中有一个新需求,用户在第一次使用app的时候要能够随机生成用户名和密码,并对用户名和密码的格式要求和长度做了限制,所以根据需求写了这个工具类.

该工具类,可以生成以下要求的随机字符串:
 1.指定长度的字符串,仅包含数字;
 2.指定长度的字符串,仅包含字母;
 3.指定长度的字符串,必须是数字+字母的组合;

另外也写上了对用户和密码的校验功能.
package com.test;

import java.util.Random;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RandomStringUtil {

    public static void main(String []args){
        System.out.print(randomString(6,6,3));
        System.out.println(matchAccountPattern("Liu123456"));

    }

    private static Object mLock = new Object();
    private static Random random = null;
    private static char[] numbersAndLetters = null;
    public static final int TYPE_UNDEFINE = 0;//undefine
    public static final int TYPE_NUMBER = 1;//numbers only
    public static final int TYPE_LETTER = 2;//letters only
    public static final int TYPE_BOTH = 3;// contains both number and letter
    public static String randomString(int minLength,int maxLength,int type){
         String targetString =null;
        if (minLength < 1) {
            return targetString;
        }
        if (random == null) {
            synchronized (mLock) {
                if (random == null) {
                    random = new Random();
                }
            }
        }

        switch (type) {
            case TYPE_NUMBER:
                numbersAndLetters = ("0123456789").toCharArray();
                break;
            case TYPE_LETTER:
                numbersAndLetters = ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
                break;
            case TYPE_UNDEFINE:
            case TYPE_BOTH:
            default:
                numbersAndLetters = ("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ").toCharArray();
                break;
        }

        //字母 65-90 97-122  数字 48-57
        boolean isNumber = false;
        boolean isLetter = false;

        int len = getStringLength(minLength, maxLength, random);
        char[] randBuffer = new char[len];
        for (int i = 0; i < randBuffer.length; i++) {
            randBuffer[i] = numbersAndLetters[random.nextInt(numbersAndLetters.length)];
            if (randBuffer[i] >= 48 && randBuffer[i] <= 57) {
                isNumber = true;
                System.out.println(i+"  randBuffer[i]  "+randBuffer[i]+"  isNumber "+isNumber);
            }
            if ((randBuffer[i] >= 65 && randBuffer[i] <= 90)||(randBuffer[i] >= 97 && randBuffer[i] <= 122)) {
                isLetter = true;
                System.out.println(i+"  randBuffer[i]  "+randBuffer[i]+"  isLetter "+isLetter);
            }
        }
        targetString = new String(randBuffer);
        if (type == TYPE_BOTH) {
            if (!isNumber||!isLetter){
                targetString= randomString(maxLength, minLength, type);
            }
        }
        return targetString;
    }
    /**
     * 获取在指定要求的长度范围内的字符长度  随机的
     *
     * @param minLength 最小长度
     * @param maxLength 最大长度
     * @param random    获取随机数的对象
     * @return 长度
     */
    private static int getStringLength(int minLength, int maxLength, Random random) {
        if (maxLength < minLength) {
            return 0;
        }
        int[] lengthArray = new int[maxLength - minLength + 1];
        for (int a = 0; a <= maxLength - minLength; a++) {
            lengthArray[a] = minLength + a;
        }
        return lengthArray[random.nextInt(lengthArray.length)];
    }

    //验证密码 6-16位  数字+字母组合
    public static boolean matchPassWordPattern(String userAccount) {
        String regex = "^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]{6,16})$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(new String(userAccount));
        return m.matches();
    }
    //8-12位,数字、字母,可以是纯数字、纯字母或字母+数字组合;  ^[a-z0-9A-Z]{8,12}$
    public static boolean matchAccountPattern(String userAccount) {
        String regex = "^[a-z0-9A-Z]{8,12}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher m = pattern.matcher(new String(userAccount));
        return m.matches();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值