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:只要字母 ...
随机推荐
- react实战项目-很适合进阶
前言 前段时间学习完了React的基础,自己网上找了一些实战项目,做了几个感觉项目不是很全面,就想做一个完整的项目来提升自己的React水平.以前学习Vue的时候,就看过bailicangdu大神的v ...
- SpringCloud核心教程 | 第二篇: 使用Intellij中的maven来快速构建Spring Cloud工程
spring cloud简介 spring cloud 为开发人员提供了快速构建分布式系统的一些工具,包括配置管理.服务发现.断路器.路由.微代理.事件总线.全局锁.决策竞选.分布式会话等等.它运行环 ...
- 解决使用SecureCRT不能连接Ubuntu的问题
一.现象 SecureCRT是远程登陆工具及串口,可以远程进行登陆Linux服务器或者串口打印数据.但我下载安装了之后想通过SecureCRT来远程登陆我的Ubuntu,出现一直连接不上. 二.问题原 ...
- mysql 获取自增id的值的方法
原生jdbc方式: Statement.getGeneratedKeys() 示例: Statement stmt = null; ResultSet rs = null; try { // // C ...
- select into from 与 insert into select 区别
1.INSERT INTO SELECT语句 语句形式为:Insert into Table2(field1,field2,...) select value1,value2,... from Tab ...
- 利用formdata对象上传文件时,需要添加的参数
function doUpload() { var formData = new FormData($( "#uploadForm" )[0]); $.ajax({ url: 'h ...
- Flask项目之手机端租房网站的实战开发(七)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- 人工智能计算器AI Calculator 3.3.0 具体破解思路&教程
人工智能计算器AI Calculator 3.3.0 具体破解思路&教程 [文章标题]:人工智能计算器AI Calculator 3.3.0 具体破解思路&教程 [文章作者]: Eri ...
- 多线程在python中的使用 thread
近期想学习研究一下python中使用多线程,来提高python在爬虫项目中的效率. 如今我们在网页上查询到在python中使用的多线程的使用大多数都是使用的threading模块,可是python中另 ...
- Cscope how to support java and c++
Cscope 首先在文件夹下建立cscope索引文件 find -name '*.c' > cscope.file cscope -Rbkq 这个命令会生成三个文件:cscope.out, cs ...