随机生成字母数字的组合
来源:互联网 发布:go高级编程书籍 编辑:程序博客网 时间:2024/05/17 00:04
随机数和随机字符串在开发中频繁的使用,本篇的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
- 随机生成字母数字的组合
- JS生成随机的由字母数字组合的字符串
- C++ 随机生成数字和字母组合的字符串
- 工具类 - 随机生成字母和数字的组合
- Java随机生成字母数字组合的验证码
- C#生成8位字母数字组合随机的字符串
- 生成6位随机数字字母组合
- java生成随机数字和字母组合
- java生成随机数字和字母组合
- java生成随机数字和字母组合
- java生成随机数字和字母组合
- java生成随机数字和字母组合
- java生成随机数字和字母组合
- php 随机生成数字字母组合
- 随机生成几位数字字母组合
- php 随机生成数字字母组合
- 生成随机数字字母组合参数
- web前端js中随机生成指定位数的字母数字、组合字符串可选择字母大小写
- Problem 6 Sum square difference
- Java 常见小问题源代码 掷骰子 游戏 三个骰子,约定三个骰子的点数1-6
- 剑指offer:第32题从1到n整数中1出现的次数
- java compareTo()
- 【多线程应用2】CreateThread与_beginthreadex本质区别
- 随机生成字母数字的组合
- 函数签名(方法签名)Type signature
- javascript1
- 海思IVE函数使用-1 (DMA使用 HI_MPI_IVE_DMA)
- 关于java和python的环境搭建问题
- C语言重点知识复习2
- 面向对象6---抽象
- [并行计算] 1. 并行计算简介
- 类型转化