随机生成字母数字的组合


 随机数和随机字符串在开发中频繁的使用,本篇的demo可以直接封装成为一个实体类,使用的时候直接调用里面的对应方法即可

import java.util.Random;
 
public class StringUtil {
    private static final int Num_WORD = 1;//数字
    private static final int STR_WORD = 2;//字母
    private static final int STR_NUM_WORD = 3;//字母数字
    private static final int MIX_WORD = 4;//字母数字符号
 
    /**
     * 获取随机字符串
     * @param style  输出格式  1纯数字,2纯字符串,3字符数字组合,4字符数字符号组合。
     * @param length 输出长度
     * */
 
    public static String getRandomStr(int style, int length) {
 
         if (style == Num_WORD) {
                return getNumRandom(length);
            } else if (style == STR_WORD) {
                return getStrRandom(length);
            } else if (style == STR_NUM_WORD) {
                return getStrNumRandom(length);
            }else if (style == MIX_WORD) {
                return getMixRandom(length);
            }else{
 
                 return getMixRandom(length);
 
             }
      
    }
 
    //纯数字
    private static String getNumRandom(int length) {
        int[] array = new int[length];
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < length; i++) {
            array[i] = (int) (Math.random() * 10);
            str.append(array[i]);
        }
        return str.toString();
    }
 
    //纯字母
    private static String getStrRandom(int length) {
        int[] array = new int[length];
        char[] chars = new char[length];
        StringBuilder str = new StringBuilder();
        for (int i = 0; i < length; i++) {
            while (true) {
                array[i] = (int) (Math.random() * 1000);
                if ((array[i] > 64 && array[i] < 91)
                        || (array[i] > 96 && array[i] < 123))
                    break;
            }
            chars[i] = (char) array[i];
            str.append(chars[i]);
        }
        return str.toString();
    }
 
    //字母数字组合
    public static String getStrNumRandom(Integer length) { 
        StringBuilder str = new StringBuilder();
        Random random = new Random(); 
        for (int i = 0; i < length; i++) { 
            boolean b = random.nextBoolean(); 
            if (b) { // 字符串 
                int choice = random.nextBoolean() ? 65 : 97; //取得65大写字母还是97小写字母 
                str.append((char) (choice + random.nextInt(26)));// 取得大写字母 
            } else { // 数字 
                str.append(random.nextInt(10)); 
            } 
        } 
        return str.toString(); 
    } 
     
    //字母数字符号组合
    private static String getMixRandom(int length) {
        int[] array = new int[length];
        char[] chars = new char[length];
        StringBuilder str = new StringBuilder();
 
        for (int i = 0; i < length; i++) {
            while (true) {
                array[i] = (int) (Math.random() * 1000);
                if (array[i] > 47 && array[i] < 91 || (array[i] > 96 && array[i] < 123))
                    break;
            }
            chars[i] = (char) array[i];
            str.append(chars[i]);
        }
        return str.toString();
    }
     
    public static void main(String[] args) {
        System.out.println(StringUtil.getRandomStr(4, 10));
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值