上一篇是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. Echarts Y轴min显示奇葩问题(做此记录)

    项目需求是根据不同情况显示最大值最小值   有9-35  12-50 9-50 三种情况 前面两种可以显示出来  但是9-50的话  最小值9显示不出来  8,7等就可以显示出来 后面想到强制从最小值 ...

  2. 【Codeforces Round #451 (Div. 2) D】Alarm Clock

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 尺取法+二分. 类似滑动窗口. 即左端点为l,右端点为r. 维护a[r]-a[l]+1总是小于等于m的就好. (大于m就右移左端点) ...

  3. C语言速度优化之指针赋值与if推断

    近期在写的一个项目须要优化处理速度,我写了一下程序来測试指针赋值与指针推断的速度比較.结果让我大吃一惊. #include <stdio.h> #include <stdlib.h& ...

  4. Android 小米盒子游戏手柄按键捕获 - 能获取到的 home 键依旧是个痛

    Android 小米盒子游戏手柄按键捕获 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致"创作公用协议 ...

  5. magento getCarriers 分析

    完整的设置订单追踪信息的时候我们可能会用到它.在后台中他在这里设置: 有的时候我们想要设置自己定义的 carrier 比如 顺丰 申通 圆通 ..等等 我们能够先从 magento api 入手分析 ...

  6. android adb常见问题的解决方法!

    ** adb的常见问题     adb:android debug bridge,用于连接模拟器/手机与PC端软件(比如:eclipse或者xx手机助手)     adb devices -> ...

  7. JS /CSS 实现模态框(注册和登录组件)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  8. python报错Nonetype object is not iterable

    https://www.cnblogs.com/zhaijiahui/p/8391701.html 参考链接:http://blog.csdn.net/dataspark/article/detail ...

  9. LeetCode Algorithm 07_Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 click to ...

  10. 写给自己的TypeScript 入门小纲

    前几日,在知乎上写了一些技术类的文章,有人私信问我,是不是要找一份工作,有没有想过要跳槽,然后我回到,你们公司都是用的什么框架什么技术,他罗列了一堆,其中就包含了TypeScript,我甚至不知道有这 ...