上一篇是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的更多相关文章

  1. commons -lang(2) RandomStringUtils RandomUtils

    上一篇是StringUtils 链接http://www.cnblogs.com/tele-share/p/8060129.html 1.RandomStringUtils 1.1模拟实现random ...

  2. commons - lang(1) StringUtils

    分享几个关于StrngUtils的几个实用的方法(以下方法中省略了参数) 1.isBlank() 这个方法用来判空,包括null和空字符串,之前自己写的时候都是str != null &&am ...

  3. 20181108 Apache Commons Lang

    工具类 org.apache.commons.lang3 AnnotationUtils ArchUtils ArrayUtils BooleanUtils CharSetUtils CharUtil ...

  4. NumberUtils、ArrayUtils和RandomUtils工具类用法

    一.NumberUtils工具类 /*1.NumberUtils.isNumber():判断字符串是否是数字*/ NumberUtils.isNumber("5.96");//结果 ...

  5. Java 数字数组随机数工具类 NumberUtils、ArrayUtils、RandomUtils用法

    commons-lang3-3-3.8.1 //----------------------------------------------------------------------- /** ...

  6. RandomStringUtils

    System.out.println(RandomStringUtils.random(5));//随机多少个随机字符中文环境乱码 System.out.println(RandomStringUti ...

  7. RandomStringUtils的使用

    //产生5位长度的随机字符串,中文环境下是乱码 RandomStringUtils.random(5); //使用指定的字符生成5位长度的随机字符串 RandomStringUtils.random( ...

  8. RandomStringUtils工具类

    //产生5位长度的随机字符串,中文环境下是乱码 RandomStringUtils.random(5); //使用指定的字符生成5位长度的随机字符串 RandomStringUtils.random( ...

  9. RandomStringUtils工具类(java随机生成字符串)

    使用RandomStringUtils可以选择生成随机字符串,可以是全字母,全数字,自定义生成字符等等... 其最基础的方法: 参数解读: count:需要生成的随机串位数 letters:只要字母 ...

随机推荐

  1. 【arc062e】Building Cubes with AtCoDeer

    Description STL有n块瓷砖,编号从1到n,并且将这个编号写在瓷砖的正中央: 瓷砖的四个角上分别有四种颜色(可能相等可能不相等),并且用Ci,0,Ci,1,Ci,2,Ci,3分别表示左上. ...

  2. php学习笔记5

    PHP 常量 常量值被定义后,在脚本的其他任何地方都不能被改变. 一个常量由英文字母.下划线.和数字组成,但数字不能作为首字母出现. (常量名不需要加 $ 修饰符). 注意: 常量在整个脚本中都可以使 ...

  3. 你真的懂Flask中浅谈蓝图Blueprint吗?

    一,什么是Flask中的蓝图Blueprint Blueprint是用于实现Flask框架中单个应用的视图,模板,静态文件的集合. Blueprint 是一个存储操作(路由映射)方法的容器,这些操作在 ...

  4. 【习题 7-1 UVA-208】Firetruck

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 预处理一下终点能到达哪些点. 暴力就好. 输出结果的时候,数字之间一个空格.. [代码] /* 1.Shoud it use lon ...

  5. 洛谷 P2067 Cytus-Holyknight

    P2067 Cytus-Holyknight 题目背景 本人最初作 以此纪念伟大的ios.安卓.PSV平台音乐游戏<cytus> 后续将不断更新. -------------Chapter ...

  6. C#调用第三方ocx控件 (winform /aspx)

    C#调用第三方ocx控件   1..net环境在工具箱上点右键,选择自定义工具箱,然后选择你需要的COM或者OCX控件就可以了. 2.在自定义工具箱中加入相应的控件,设置id,在客户端脚本中直接引用它 ...

  7. 2013腾讯编程马拉松初赛第〇场(HDU 4503) 湫湫系列故事——植树节

    http://acm.hdu.edu.cn/showproblem.php?pid=4503 题目: 已知湫湫的班里共有n个孩子,每个孩子有Bi个朋友(i从1到n),且朋友关系是相互的,如果a小朋友和 ...

  8. python3 随机生成6位数的验证码

    python3 随机生成6位数的验证码 要求是数字:0~9 及大小写字母. #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung ...

  9. 手机用appnium,web自动化用eclips+webdriver2

    手机用appnium,web自动化用eclips+webdriver2 吴建清 pycharm 1.安装环境2.pycharm类似eclipse,写脚本,运行脚本3.uiautomatorviewer ...

  10. UVA 10603 - Fill BFS~

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&c ...