RandomStringUtils RandomUtils
上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html
1.RandomStringUtils

1.1模拟实现random(count,str);

1 //模拟实现random(5,"helloworld")
2 public static String getRandomString(int count,String str) {
3 if(str != null) {
4 if(!StringUtils.isBlank(str)) {
5 if(count <= 0) {
6 return "";
7 }
8 char[] charArray = str.toCharArray();
9 int index;
10 char[] newArray = new char[count];
11 for(int i=0;i<count;i++) {
12 index = RandomUtils.nextInt(0,charArray.length);
13 newArray[i] = str.charAt(index);
14 }
15 return new String(newArray);
16 }
17 }
18 return null;
19 }

1.2模拟实现randomAlphanumeric(字母与数字混合,可能没有数字)

1 //模拟实现randomAlphanumeric(字母与数字混合)
2 public static String getRandomAlphanumeric(int count) {
3 if(count <=0) {
4 return "";
5 }
6 String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
7 int number = RandomUtils.nextInt(0,100000);
8 String str = letters + number;
9 return getRandomString(count, str);
10 }

1.3模拟实现可以指定数字个数的randomAlphanumeric

1 //指定数字的个数
2 public static String getRandomAlphanumeric(int count,int numbers) {
3 if(count <=0 || numbers <=0) {
4 return "";
5 }
6 //纯数字
7 if(numbers>=count) {
8 return RandomStringUtils.randomNumeric(numbers);
9 }
10 String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
11 String str = RandomStringUtils.random(count-numbers, letters) + RandomStringUtils.randomNumeric(numbers);
12 //打乱位置
13 char[] charArray = str.toCharArray();
14 List<Character> list = new ArrayList<Character>();
15 for (Character character : charArray) {
16 list.add(character);
17 }
18 Collections.shuffle(list);
19 for(int i=0;i<list.size();i++) {
20 charArray[i] = list.get(i);
21 }
22 return new String(charArray);
23 }
24

总结:RandomStringUtils中的方法可以用于生成随机的验证字符串
2.RandomUtils
这个类感觉与java.util包下的Random类差别不大,还是那几个类似的方法(注意左闭右开)

相比较来说还是RandomStringUtils用处更多一点
RandomStringUtils RandomUtils的更多相关文章
- commons -lang(2) RandomStringUtils RandomUtils
上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html 1.RandomStringUtils 1.1模拟实现random ...
- commons - lang(1) StringUtils
分享几个关于StrngUtils的几个实用的方法(以下方法中省略了参数) 1.isBlank() 这个方法用来判空,包括null和空字符串,之前自己写的时候都是str != null &&am ...
- 20181108 Apache Commons Lang
工具类 org.apache.commons.lang3 AnnotationUtils ArchUtils ArrayUtils BooleanUtils CharSetUtils CharUtil ...
- NumberUtils、ArrayUtils和RandomUtils工具类用法
一.NumberUtils工具类 /*1.NumberUtils.isNumber():判断字符串是否是数字*/ NumberUtils.isNumber("5.96");//结果 ...
- Java 数字数组随机数工具类 NumberUtils、ArrayUtils、RandomUtils用法
commons-lang3-3-3.8.1 //----------------------------------------------------------------------- /** ...
- RandomStringUtils
System.out.println(RandomStringUtils.random(5));//随机多少个随机字符中文环境乱码 System.out.println(RandomStringUti ...
- RandomStringUtils的使用
//产生5位长度的随机字符串,中文环境下是乱码 RandomStringUtils.random(5); //使用指定的字符生成5位长度的随机字符串 RandomStringUtils.random( ...
- RandomStringUtils工具类
//产生5位长度的随机字符串,中文环境下是乱码 RandomStringUtils.random(5); //使用指定的字符生成5位长度的随机字符串 RandomStringUtils.random( ...
- RandomStringUtils工具类(java随机生成字符串)
使用RandomStringUtils可以选择生成随机字符串,可以是全字母,全数字,自定义生成字符等等... 其最基础的方法: 参数解读: count:需要生成的随机串位数 letters:只要字母 ...
随机推荐
- CISP/CISA 每日一题 16
CISA 每日一题(答) 作业调度软件的优点: 1.作业信息仅需建立一次,减少错误发生概率: 2.可定义作业间的依赖关系,当某一项作业失败时,依赖于该作业的后续作业就不会被执行: 3.所有成功或失败的 ...
- 【hdu 3478】Catch
[Link]:http://acm.hdu.edu.cn/showproblem.php?pid=3478 [Description] 一个人从起点s出发,假设他在时间t在节点x; 则在时间t+1,他 ...
- POJ 3009 Curling 2.0 {深度优先搜索}
原题 $On Planet MM-21, after their Olympic games this year, curling is getting popular. But the rules ...
- mysql-cacti-templates-1.1.2.tar.gz 免费下载 cacti MySQL添加监控
cacti MySQL添加监控 1. 安装监控插件 wget http://mysql-cacti-templates.googlecode.com/files/mysql-cacti-templat ...
- hard-negative mining 及伪代码实现
Histogram of Oriented Gradients and Object Detection 获得 records 对于目标检测(object detection)问题,所谓的 hard- ...
- JS/CSS 响应式样式实例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Python 极简教程(十三)while 循环
循环简单来说就是让一段代码按你想要的方式多次运行.软件拥有强大的运算能力,就是由循环提供的. 在 Python 中支持的循环由两种:while 循环 和for 循环. 现在我们先来讲while循环. ...
- pt模型
top-down
- COdeVS——T 1082 线段树练习 3 (分块练习)
http://codevs.cn/problem/1082/ 时间限制: 3 s 空间限制: 128000 KB 题目等级 : 大师 Master 题解 题目描述 Descriptio ...
- 洛谷 P1644 跳马问题
P1644 跳马问题 题目背景 在爱与愁的故事第一弹第三章出来前先练练四道基本的回溯/搜索题吧…… 题目描述 中国象棋半张棋盘如图1所示.马自左下角(0,0)向右上角(m,n)跳.规定只能往右跳,不准 ...